Doctrine Query Builder - How to choose many-to-many relationships?

I have a form in Symfony2 that needs to have an entity selection field populated by the Players collection in a specific tournament. I create a Type form and pass the tournament identifier to the query_builder property of the correct field. In the object repository, I have this method:

public function allPlayersInTournamentQuery($tournament_id) { $repo = $this->getEntityManager()->getRepository('GameBundle:Tournament'); $tournament = $repo->find($tournament_id); $players = $tournament->getPlayers(); $playersIds = array(); foreach ($players as $player) { $playersIds[] = $player->getId(); } $playersQuery = $this->createQueryBuilder('p') ->in('p.id', $playersIds) ->orderBy('p.real_name', 'ASC'); return $playersQuery; } 

The function in () does not exist in the query builder. Hope the method shows what I'm trying to do. Im binding to return a query builder who selects the correct players found in this tournament.

How can i achieve this?

Thanks!

+4
source share
1 answer

You can use the helper methods provided by the query builder $playersQuery->expr()->in('p.id', $playersIds)

Your query will look something like this:

 $playersQuery = $this->createQueryBuilder('p'); $playersQuery->where($playersQuery->expr()->in('p.id', $playersIds)) ->orderBy('p.real_name', 'ASC'); 

Read more about helper methods here.

+3
source

Source: https://habr.com/ru/post/1393898/


All Articles