1

I would like to test the SQL Builder library atk4/dsql with Symfony 5 and am trying to set up the database connection. I tried following these instructions in the official docs.

I do not have enough experience with Symfony to find out how to set up a global class so the Atk4 library can use the database connection details (I did enter the db details in the .env file).

I have code like this:

<?php
namespace App\Repository;

use Atk4\Dsql\Query;

class UserRepository
{
    public function getUser($username) {
        $query = new Query();
        $query  ->table('user')
                ->where('username', $username)
                ;

        // Debug Output
        // print_r($query); die;
        return $query->getOne();
    }
}

But when running this I only get "Call to a member function execute() on null".

I have checked this (fantastic) collection of explanations for PHP Errors, but could not find one for this problem. The closest I could find is "Fatal error: Call to a member function ... on a non-object or null". There it explains that this error could occur if - as in the example above - $query were not an object. But $query is definitely an object (I got double confirmation by using gettype($query)).

Therefore I assume that it is my missing database connection definition.

How do I set up the database connection and make DSQL use it for every query in Symfony 5? Or am I possibly overlooking something else?

Charles
  • 179
  • 1
  • 15
  • And where is code you use for creating database connection object and actually make connection to database? – DarkSide May 05 '21 at 16:12
  • That is exactly my question. Where do I set this up? I set up the database connection details in the .env file. – Charles May 06 '21 at 00:07

1 Answers1

1

Well, first you have to setup Connection for your database and described here https://dsql.readthedocs.io/en/develop/connection.html

$connection = \atk4\dsql\Connection::connect($dsn, $user, $pass);

and then pass this Connection object to Query or initialize query from Connection object.

// initialize Query object from Connection object
$query = $connection->dsql();

or in your specific case you can do something like this to pass connection to your query

use Atk4\Dsql\Connection;

class UserRepository
{
    /** @var Connection */
    public $connection;

    public function __construct(Connection $connection) {
        $this->connection = $connection;
    }

    public function getUser(string $username): ?array {
        return $this->connection->dsql()
            ->table('user')
            ->where('username', $username)
            ->getRow();
    }
}

// usage
$connection = \atk4\dsql\Connection::connect($dsn, $user, $pass);
$repo = new UserRepository($connection);
var_dump($repo->getUser('John'));

P.S. If you want to return just user Id or some other field, then you can use getOne() method, but have to also use field($fieldname) method to define which single field to select.

P.P.S. Where and how you get your $dsn, $user and $pass from ENV file or any other place where you set them up is up to you.

DarkSide
  • 3,670
  • 1
  • 26
  • 34
  • Thanks. This brought me on the correct path. Obviously I am still starting out. Appreciate the help. – Charles May 10 '21 at 06:46
  • 1
    If you're just starting up then please start with 3.0 version which we released today. It contains some major breaking changes. That way your new project will be more "future proof" and easier to keep up with changes in ATK in future :) Example above should still work in 3.0 except that atk4/dsql is now merged into atk4/data, so you have to require atk4/data in your composer.json file. – DarkSide May 11 '21 at 20:44
  • Will keep that in mind and will take a look right away. – Charles May 13 '21 at 12:28