Y
Y
Yuri Petrashevich2016-08-14 12:09:50
symfony
Yuri Petrashevich, 2016-08-14 12:09:50

How to get data from ManyToMany in createQueryBuilder?

$qbRows = $this
            ->createQueryBuilder('b')
            ->where('b.id = ' . (int) $id)
            ->select('b.id')
            ->addSelect('b.name')
            ->addSelect('b.Users')
            ->getQuery();

b.Users should be a collection of users. Without specifying select (addSelect), all fields are retrieved, and everything is OK. But when selecting only some fields, it is not clear how to display the fields that are connected through links.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Skobkin, 2016-08-14
@skobkin

If you get not the whole entity, but only specific fields, obviously, you need to do a JOIN and access the relationship fields using the alias that you assign in the JOIN.

A
Andrey Kulikovsky, 2016-08-15
@by25

If you unload entities completely:

$query= $this->
            ->createQueryBuilder()
            ->select('b, users')
            ->from(B::class, 'b')
            ->join('b.users', 'users')
            ->where('b.id = :id')
            ->setParameter('id', $id)
            ->getQuery();

Pay attention to the line select('b, users')
PS. And Doctrine is quite a complex tool, as advised above - read the documentation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question