0
votes

I am trying to code my own login script in PHP 7, using a MySQL database and Doctrine 2 as ORM. Unfortunately, when I try to fetch data from database (e.g. users login accounts by login-name) I get a Doctrine\ORM\Query\QueryException.

I follow the code first approach and have my entity for user accounts as follows, as well the Doctrine 2 entity manager. Is there anything I miss? Fetching data with simple ->findall() works properly.

The query I tried to build:

$query = DatabaseManager::_instance()->createQuery("SELECT a FROM " . LoginAccountDTO::class . " a WHERE a.username = ?1");

or using the Query Builder:

$qb = DatabaseManager::_instance()->createQueryBuilder();
$qb->select('account')
    ->from(LoginAccountDTO::class, 'account')
    ->where('account.username = ?1 OR account.email = ?1')
    ->setParameter(1, $username);

(as well tested with doctrine's expr()).

The DQL created is as follows:

"SELECT a FROM Entities\LoginAccountDTO account WHERE account.username = ?1 or account.email = ?1"

Now, each time I try to ->getResult() of the query, I got the following error output:

Fatal error: Uncaught Doctrine\ORM\Query\QueryException: SELECT a FROM Entities\LoginAccountDTO a WHERE a.username = ?1 in (project-dir)\vendor\doctrine\orm\lib\Doctrine\ORM\Query\QueryException.php:43 Stack trace: #0 (project-dir)\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(487): Doctrine\ORM\Query\QueryException::dqlError('SELECT account FROM K...') #1 (project-dir)\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(987): Doctrine\ORM\Query\Parser->semanticalError('line 0, col 14 ...', Array) #2 (project-dir)\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(1728): Doctrine\ORM\Query\Parser->validateAbstractSchemaName('Entities\Accoun') #3 (project-dir)\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(1578): Doctrine\ORM\Query\Parser->RangeVariableDeclaration() #4 (project-dir)\ve in (project-dir)\vendor\doctrine\orm\lib\Doctrine\ORM\Query\QueryException.php on line 65

The configuration for Doctrine should be correct, because if I try to fetch data via ->findAll(), I can see the data stored. As well the entity are created correctly in the database.

I tried to use Doctrine 2(.6.0) like described in documentation, but without success. Can please anyone help me to fetch data from from database with a where clause in Doctrine's query string/prepared query?

1

1 Answers

0
votes

I found out, that the solution for this issue is quiet embarresing to me: The problem was cuased by missing import statement in class that used the entity class. Doctrine just respond with a Fatal Error.

The sulution in a nutshell: Always import the entities that will be used by the class.