在我的實體主義的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));' –