How to retrieve only entity id using Symfony QueryBuilder?
Asked Answered
C

4

7

I'm trying to personalize a symfony 2.4 repository query to retrieve only some fields. Everything is ok with flat fields but when retrieving Entity fields, I only get the id (by default) but not the whole entity data. My query:

    $select = $this->createQueryBuilder('ca')
    ->select('ca.id, ca.name')
    ->leftJoin('ca.users', 'user')
    ->addSelect('(user) as users'); 

    $select->setMaxResults($count);

    return $select->getQuery()->getResult();

The result is: [{id: 1, name: "Some name", users: 1}, ...]

How can I change this query for users to contain the whole user data, like id, name, address, etc.?

Cooney answered 31/5, 2018 at 10:5 Comment(7)
What field does your user entity contain?Ouster
@Ouster a many-to-many user relation, if you mean that.Cooney
No, i thought fields. Id, name and what else ? Maybe add code with your both entity classes, so we can check whats wrong. Currently looking at your code, everything, except ->addSelect seems okay. Also are you sure, that you need ->leftJoin in your example :) ? With many to many usually its inner join to get needed elements for some idOuster
No, it needs to be a leftJoin for sure, as innerJoin deletes some entries without user. The entities doesn't have anything special: a simple class with no extends and simple private fields like id, name, status... and public methods. Also, other fields joining entities have the same problem.Cooney
For me in 2.4 you dont have to specify select to get the whole user data just add your join, but i dont know how work getArrayResult().Kedgeree
getArrayResult() will not return back related entities resulting in a flat structure. Changing it to getResult() will allow you to return and inspect the entire entity and its linked data. Unless thats what you want? A cartesian product of multiple joins?Midian
@NeilMasters I'm sorry to tell you that using getResult I'm getting the same flat result. I'm getting only id instead of all user data.Cooney
P
4

This works for me:

$select = $this->createQueryBuilder('ca')
    ->select('partial ca.{id, name}, users')
    ->leftJoin('ca.users', 'users');

$select->setMaxResults($count);

return $select->getQuery()->getArrayResult();
Perfectible answered 5/6, 2018 at 13:39 Comment(0)
D
2

You should try this way. You can use Partial.

$select = $this->createQueryBuilder('ca')
->select('partial ca.{id, name}')
->leftJoin('ca.users', 'users')
->addSelect('users'); 

A detailed description of this issue is available here. Doctrine2: Cannot select entity through identification variables without choosing at least one root entity alias

Diluent answered 5/6, 2018 at 16:24 Comment(1)
This result is the same as doing only $select = $this->createQueryBuilder('ca') It is returning the whole instance with every join, which I'm trying to avoidCooney
C
1

change this:

->select('ca.id, ca.name')

to this:

->select('ca')
Coreycorf answered 31/5, 2018 at 10:6 Comment(5)
Done, but it doesn't solve the problem. The result is [ { 0: { id:1, name:"Some name" }, 'users': 1}, { }...]Cooney
Have you tried to delete cache? And inside the entity there is some kind of exclusion policy? @CooneyCoreycorf
I've cleared cache and also checked the dump() result instead of the serialized value. The problem is already there.Cooney
Did you try addSelect('user') or ->leftJoin('ca.users', 'users') and addSelect('users')Diluent
@RamazanApaydın yes and this is the error displayed: Cannot select entity through identification variables without choosing at least one root entity alias Cooney
F
1

i hope this will useful for you.

$qb = $this->em->createQueryBuilder();
        $qb->select('o.id as order_id,o.createdBy as created_by,o.user as user_id');
        // for getting selective fields
        //$qb->select('o');        
        //for getting all fields in certain table
        $qb->from('Entity\Orders', 'o');
        $qb->setMaxResults(1); 
        // for getting single record only.
        return $qb->getQuery()->getOneOrNullResult(); 
        // for getting single record or null record.
        //return $qb->getQuery()->getResult(); 
        // for getting multiple records.  

feel free to ask any query regarding it.

Ferryboat answered 8/6, 2018 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.