1

我在事件和参与者实体之间存在单对立关系。在我的控制器中,我可以执行以下操作:

$participants = $event->getParticipant();

但现在我只想要属性visible值为 1 的参与者。

我怎样才能得到那些收藏?因为参与者是participant.event_id = event.id事件实体中所有参与者的数组集合。

4

1 回答 1

1

您可以轻松地在您的方法中创建一个EventRepository仅将可见参与者加入事件的方法:

// YourBundle/Entity/EventRepository.php

public function getEventFilterVisibleParticipants($event_id)
{
    return $repository->createQueryBuilder('event')
        ->where('event.id = :event_id')
        ->leftJoin('event.participants', 'participant', 'WITH', 'participant.visible = :visibility')
        ->setParameter('event_id', $event_id)
        ->setParameter('visibility', 1)
        ->orderBy('event.startDate', 'DESC')
        ->getQuery()
        ->getResult()
    ;
}

...现在在您的控制器中执行以下操作:

$event = $this
    ->getDoctrine()
    ->getRepository('YourBundle:Event')
    ->getEventFilterVisibleParticipants($id)
;

$participants = $event->getParticipants(); // returns the filtered collection
于 2013-10-26T13:11:14.223 回答