Answer the question
In order to leave comments, you need to log in
How to select specific columns in relationships?
There are 3 entities with one-to-many relationships (let there be a user, books and book authors). I want to select only the user, an array of books that he reads and the names of the authors of the books. Accordingly, the user, books and the author have a bunch of fields that I don’t need and I don’t want to rake everything from the database.
Actually, if I do this
public function findByUserId($id)
{
$query = $this->createQueryBuilder('u');
$query->select('u,b,a')
->leftJoin(
'u.books',
'b',
Join::WITH,
'b.id = g.userId'
)
->leftJoin(
'b.authors',
'a',
Join::WITH,
'a.bookId = b.id'
);
return $query->getQuery()->getOneOrNullResult();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question