在我的实体主义的QueryBuilder凡在收集
/**
* @ORM\ManyToMany(targetEntity="\UserBundle\Entity\User", mappedBy="acr_groups")
*/
protected $users;
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
在我FormType我要过滤掉那些基团,其中当前用户是成员,我有用户的一个数组集合:
$builder
->add('acr_group', EntityType::class, array(
'label' => 'ATS',
'class' => 'HazardlogBundle:ACRGroup',
'query_builder' => function (EntityRepository $er) use ($user) { // 3. use the user variable in the querybilder
$qb = $er->createQueryBuilder('g');
$qb->where(':user IN (g.users)');
$qb->setParameters(array('user' => $user));
$qb->orderBy('g.name', 'ASC');
return $qb;
},
'choice_label' => 'name'
))
我问题显然是在这条线:
$qb->where(':user IN (g.users)');
我如何使用我的用户的集合作为参数IN()?
您好!你可以尝试使用'in'表达式:'$ qb-> where($ qb-> expr() - > in('g.users',$ user));' –