问题描述:

In my Padel project on Symfony2 I have a Competition - Registration relationship

On the "Competition" entity:

 /**

* @ORM\OneToMany(targetEntity="Registration", mappedBy="competition")

*/

protected $registrations;

On the "Registration" entity:

/**

* @ORM\ManyToOne(targetEntity="PadelSchedule\UserBundle\Entity\User", inversedBy="registrations")

* @ORM\JoinColumn(name="idPlayer", referencedColumnName="id")

*/

private $player;

/**

* @ORM\ManyToOne(targetEntity="Competition", inversedBy="registrations")

* @ORM\JoinColumn(name="idCompetition", referencedColumnName="id")

*/

private $competition;

Something like this. What I need to do is, having the id of the player, get a list of the competitions on which this player is registered.

The close thing that I have get is using a join with this query:

 $qb = $em->createQueryBuilder()

->select('r, c')

->from('PadelScheduleMainBundle:Registration', 'r')

->leftJoin('r.competition', 'c')

->where('r.player = :idPlayer')

->setParameter('idPlayer', $idPlayer);

But as it seems by the error I get, what I think I get through this is a list of Registrations with the competition joined, but I need for my view is a list of "Competition" objects.

Any help? Thanks!

网友答案:

Since you select from your Registration entity you get a result of Registrations. Maybe you have better luck using a query like this:

$em->createQueryBuilder()
    ->select('c')
    ->from('PadelScheduleMainBundle:Competition', 'c')
    ->innerJoin('c.registrations', 'r')
    ->where('r.player = :player')
    ->setParameter('player', $playerId)
网友答案:

According to your query question.. I have write this query ..Please Try with this query:

$query = $em->createQuery(
    'SELECT c
     FROM PadelScheduleMainBundle:Competition c
     INNER JOIN c.registrations r
     WHERE r.player = :idPlayer')
->setParameter('idPlayer', $idPlayer);
相关阅读:
Top