6

Is there a possibility to execute something like:

$builder = $this->getEntityManager()->createQueryBuilder();
$builder->select(
        'f.id',
        ...
        'NULL AS missing_attribute'
    )
    ->from(..., 'f')
    ...;

And avoid:

Doctrine\ORM\Query\QueryException: ... got 'NULL'
FieryCat
  • 1,875
  • 18
  • 28
  • maybe that will help you https://stackoverflow.com/questions/5154383/how-to-specify-null-value-as-filter-in-a-doctrine-query – Aref Ben Lazrek Sep 07 '17 at 08:07
  • 1
    عارف بن الأزرق, nope. NULL is needed as an extra field (that is missing for the original table). It's more or less near the issue: https://github.com/doctrine/doctrine2/issues/1670 – FieryCat Sep 07 '17 at 08:10
  • Add your entity code and explain about that value. Does it exist in some other storage, not in your database? – svgrafov Sep 07 '17 at 08:24
  • Entity or other inputs are not needed. Try: `SELECT NULL AS test FROM any_table_in_the_database LIMIT 1;` I want to emulate directly the same behaviour by using Doctrine 2 ORM. – FieryCat Sep 07 '17 at 08:28
  • If you just want to have a null field in result set, you can append it manually. It seems like the data is going to be sent into some external API and in this case you can create separate Adapter class that will adapt your format of data to required format. – svgrafov Sep 07 '17 at 08:45
  • [github bux/issue](https://github.com/doctrine/doctrine2/issues/1670): This seems to be a bug in doctrine 2 that is not fixed yet. – ArchLinuxTux Jun 18 '18 at 08:44

2 Answers2

1

Workaround from https://github.com/doctrine/orm/issues/1670#issuecomment-591495663: select NULLIF(1,1) AS ... instead of NULL AS ...

0

What you want could be achieved by using the DBAL connection directly and issuing a normal SQL query.

The query builder is used to generate a DQL (Domain Query Language) and this null field is not part of your domain.

Or just select it without the NULL field and run through the results and add it manually.

Goran Jurić
  • 1,829
  • 13
  • 17