Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
242bpm opened this issue Mar 30, 2023 · 8 comments

Comments

@242bpm
Copy link

242bpm commented Mar 30, 2023

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?

@Psiloscop
Copy link

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.

@olegatro
Copy link

Did anyone find a solution?

@Clement-B
Copy link

Clement-B commented Sep 19, 2023

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

@andreiyaruk
Copy link

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

@Clement-B
Copy link

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.".

@zeromodule
Copy link

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

@pfpro
Copy link
Contributor

pfpro commented Dec 12, 2023

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.

@FractalizeR
Copy link

Same problem. Looks like a bug, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants