EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

createIndexQueryBuilder fails with "Cannot read property "id" from an array. ....

Open 242bpm opened this issue 2 years ago • 9 comments

In my crud controller i use the createIndexQueryBuilder method to additionally add a calculated value to the result.

public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder { $queryBuilder = parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters); $queryBuilder->addSelect('SUM(case when s.assignedAt IS NULL then 1 else 0 end) AS free'); $queryBuilder->leftJoin('entity.serials','s'); $queryBuilder->groupBy('entity.id'); return $queryBuilder; }

In frontend ist shows the following error: Cannot read property "id" from an array. Maybe you intended to write the property path as "[id]" instead. NoSuchPropertyException

Any idea?

242bpm avatar Mar 30 '23 15:03 242bpm

The problem is that the query builder returns an array as a result, but for EasyAdmin it must be an object list. I have the same issue now and I don't know how to force query builder cast array to some object.

Psiloscop avatar May 02 '23 15:05 Psiloscop

Did anyone find a solution?

olegatro avatar Jun 19 '23 13:06 olegatro

Come here cause I got the same issue but figure out the solution.

You need to use the keyword HIDDEN in your DQL. This way, it tell doctrine to ignore this column and then return an object instead of an array.

Example:

$queryBuilder->addSelect('SUM(case when s.assignedAt IS NULL then 1 else 0 end) AS HIDDEN free');

This does not alter your database query so you can order your query by this column without any issue for example.

You can see a working example in official documentation : https://symfony.com/bundles/EasyAdminBundle/current/fields.html#unmapped-fields

Clement-B avatar Sep 19 '23 15:09 Clement-B

Come here cause I got the same issue but figure out the solution.

You need to use the keyword HIDDEN in your DQL. This way, it tell doctrine to ignore this column and then an object instead of an array.

Example:

$queryBuilder->addSelect('SUM(case when s.assignedAt IS NULL then 1 else 0 end) AS HIDDEN free');

This does not alter your database query so you can order your query by this column without any issue for example.

You can see a working example in official documentation : https://symfony.com/bundles/EasyAdminBundle/current/fields.html#unmapped-fields

TextField::new('fullName') ->addSelect('CONCAT(entity.first_name, ' ', entity.last_name) AS HIDDEN full_name') outputs null symfony 5.4

andreiyaruk avatar Sep 19 '23 16:09 andreiyaruk

TextField::new('fullName') ->addSelect('CONCAT(entity.first_name, ' ', entity.last_name) AS HIDDEN full_name') outputs null symfony 5.4

I tested it in Symfony 6.4 and had no issue.

Could you add a more detailed example ? What return null exactly ?

The error resolved with the keyword HIDDEN is when we got " Cannot read property "id" from an array. Maybe you intended to write the property path as "[id]" instead.".

Clement-B avatar Sep 20 '23 07:09 Clement-B

TextField::new('fullName')

->addSelect('CONCAT(entity.first_name, ' ', entity.last_name) AS HIDDEN full_name')

outputs null symfony 5.4

I have the same problem. SQL query is built correctly and is executed successfully, but the field value is not transferred to EasyAdmin.

Symfony 6.1, Doctrine 2.14.3

zeromodule avatar Dec 12 '23 14:12 zeromodule

I solved it by calling a new repository function where i select all existing fields, including the custom ones. (No hidden keyword neccessary)

But I would also be interested to know if there is a shorter way.

pfpro avatar Dec 12 '23 16:12 pfpro

Same problem. Looks like a bug, right?

FractalizeR avatar May 07 '24 12:05 FractalizeR