gpt4 book ai didi

symfony - Doctrine queryBuilder where IN 集合

转载 作者:行者123 更新时间:2023-12-04 15:31:40 25 4
gpt4 key购买 nike

在我的实体上,我有一个用户数组集合

/**
* @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() 的参数?

最佳答案

试试下面的代码

$user = array(12,211,612,84,63,23); // Assuming User Ids whose groups you want to retrive

$builder
->add('acr_group', EntityType::class, array(
'label' => 'ATS',
'class' => 'HazardlogBundle:ACRGroup',
'query_builder' => function (EntityRepository $er) use ($user) {
$qb = $er->createQueryBuilder('g');
$qb->innerJoin('g.users', 'u'); // Inner Join with users
$qb->where('u.id IN (:user)');
$qb->setParameters( array('user' => $user) );
$qb->orderBy('g.name', 'ASC');
return $qb;
},
'choice_label' => 'name'
))

我在 symfony 2.3 试过了与 doctrine2 .您可以使用 select功能与 createQueryBuilder()获取特定列。

关于symfony - Doctrine queryBuilder where IN 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40502052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com