Reputation: 29
I have two entities :
I would like to select just few columns of the user when I get all notes of my database (in order to don't get the password of the user for example)
So in the NoteRepository I have made a doctrine request like this :
$qb = $this->createQueryBuilder('n');
//Get the owner of the knowledge
$qb
->leftJoin('n.user', 'owner')
->addSelect('owner.tag as ownerTag, owner.firstname as ownerFirstname, owner.lastname as ownerLastname')
;
return $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
(I insist to do it with a join clause, because we can imagine more than one user like owner of the note)
And I get this following JSON response :
[
{
"0":{
"id":6,
"title":"A1",
"description":"A1",
"isPublic":false,
"ownerTag":"#02a4c022d8",
"ownerFirstname":"ama",
"ownerLastname":"ama"
}
},
{
"1":{
"id":7,
"title":"Z1",
"description":"Z1",
"isPublic":false,
"ownerTag":"#00a7bd24g8",
"ownerFirstname":"z",
"ownerLastname":"z"
}
}
]
But I would like this following response :
[
{
"0":{
"id":6,
"title":"A1",
"description":"A1",
"isPublic":false,
"owner":{
"tag":"#02a4c022d8",
"firstname":"ama",
"lastname":"ama"
}
}
},
{
"1":{
"id":7,
"title":"Z1",
"description":"Z1",
"isPublic":false,
"owner":{
"tag":"#00a7bd24g8",
"firstname":"z",
"lastname":"z"
}
}
}
]
I don't know how to get it. Thank you to helping me ;)
Upvotes: 0
Views: 1606
Reputation: 29
Alright, I found a solution. I need to use the clause PARTIAL like this:
$qb = $this->createQueryBuilder('n');
//Get the owner of the knowledge
$qb
->leftJoin('n.user', 'owner')
->addSelect('PARTIAL owner.{id,tag,firstname,lastname}')
;
return $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
Upvotes: 2
Reputation: 1108
I found this https://github.com/Atlantic18/DoctrineExtensions/issues/118
I'm not sure you're able to do it straight with your queryBuilder.
But you could surely do it with some PHP work on your array :)
Upvotes: 0