1

Is there a way to use a Doctrine reserved word in a DQL statement?

I would like to use an Entity class called "Member" but I cannot use it neither with QueryBuilder nor Query, aliases, etc.

Examples :

$qb = $entityManager->createQueryBuilder();
$qb->select('m.id')
  ->from('Member', 'm')
  ->where('m.id=1234');
return $qb->getQuery()->getArrayResult();

or

$q = $entityManager->createQuery('select m.id from Member m');
return $q->getArrayResult();

both examples give the following error:

Error: Expected Doctrine\ORM\Query\Lexer::T_ALIASED_NAME, got 'Member'

Note : the table name is not the problem since it's not Member

b126
  • 544
  • 4
  • 11
  • Just for kicks, in your first example try using `from(Member::class,` – Cerad Nov 11 '22 at 13:42
  • Already tried. Same error unfortunately. – b126 Nov 11 '22 at 15:58
  • Seems I am not the only one to use this reserved word... https://github.com/doctrine/orm/issues/7264 – b126 Nov 11 '22 at 16:03
  • 1
    I made a quick test by creating a Member entity. Did not even bother to change the table name since `member` was only a reserved word in mysql for a couple of point versions. I ran your code exactly and besides getting the lexer error I got `SELECT m.id FROM Member m WHERE m.id=1234`. Notice the capital M in the Member table name. So I used `from(Member::class,` as suggested in my first comment (note no quotes around Member::class) and it all worked. Unless your entities really have no namespace at all then using just 'Member' is not going to work. – Cerad Nov 11 '22 at 18:39
  • 1
    The issue you linked is interesting. I suspect it was also a namespace problem but you would think some of the people participating would have pointed that out. I ran my test inside of the Symfony framework with an `App\Entity\Member` entity. Your tags indicate you might be using Doctrine standalone? If `Member` really is your fully qualified class name then perhaps you could update your question with your [bootstrap configuration](https://www.doctrine-project.org/projects/doctrine-orm/en/current/tutorials/getting-started.html#obtaining-the-entitymanager) and I'll test standalone. – Cerad Nov 12 '22 at 14:14
  • Thanks @Cerad, I am indeed using Doctrine with CodeIgniter 4 (it’s a bit like a standalone then). It works if I use ‘\Member’ instead of ‘Member’. The problem was thus the namespace. Thank you! – b126 Nov 13 '22 at 15:13
  • Good to know but there is still something strange about the specific name `Member`. I made a standalone orm app following the getting started guide. The example `Product` entity worked as expected without any namespace or backslash. But `Member` did not without adding the backslash. `Memberx` worked fine. I did not search any further. Might be wise to just use a different entity name. Might avoid future issues. – Cerad Nov 13 '22 at 16:19
  • It seems Member is a reserved word in DQL. See https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#ebnf – b126 Nov 13 '22 at 16:47

0 Answers0