您可能最好使用 Doctrine 的filter
集合方法来完成此操作
http://api.nellafw.org/class-Doctrine.Common.Collections.ArrayCollection.html
所以,在你的Event
实体上,让我们假设你的关系看起来像这样
<?php
namespace /Your/Bundle/Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Table()
* @ORM\Entity()
*/
class Event
{
/**
* @ORM\OneToMany(targetEntity="Participant", mappedBy="event")
*/
protected $participants;
public function __construct()
{
$this->participants = new ArrayCollection();
}
}
所以你需要做的就是添加一个查询方法(到Event
实体),它将使用 Doctrine 集合的过滤功能
/**
* Retrieves all associated participants that are visible
*
* @return \Doctrine\Common\Collections\ArrayCollection
*/
public function getVisibleParticipants()
{
return $this->participants->filter( function( Participant $participant )
{
// Add only visible participants to the returned collection
return (boolean) $participant->getVisible();
});
}