EasyAdminBundle
EasyAdminBundle copied to clipboard
createIndexQueryBuilder fails with "Cannot read property "id" from an array. ....
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?
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.
Did anyone find a solution?
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
Come here cause I got the same issue but figure out the solution.
You need to use the keyword
HIDDENin 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
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.".
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
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.
Same problem. Looks like a bug, right?