关于symfony:教义queryBuilder其中IN集合

doctrine queryBuilder where IN collection

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

1
2
3
4
5
6
7
8
/**
 * @ORM\\ManyToMany(targetEntity="\\UserBundle\\Entity\\User", mappedBy="acr_groups")
 */
protected $users;

public function __construct() {
    $this->users = new \\Doctrine\\Common\\Collections\\ArrayCollection();
}

在我的FormType中,我想过滤出当前用户是成员的那些组:

1
2
3
4
5
6
7
8
9
10
11
12
13
    $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'
    ))

我的问题显然在这条线上:

1
$qb->where(':user IN (g.users)');

如何将我的用户集合用作IN()的参数?


尝试下面的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$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.3doctrine2中进行了尝试。您可以将select函数与createQueryBuilder()一起使用以获取特定的列。


在尝试某些解决方案失败后,我终于将事情扭转了一下。我手动创建了所需ID的数组。

可能有一种本机的方法,这似乎是很标准的事情……但是可以。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// 1. to inject user entity into this builder first make a construct function (remember to inject it from controller!)

function __construct($user)
{
    $this->user = $user;
}

/**
 * {@inheritdoc}
 */


public function buildForm(FormBuilderInterface $builder, array $options)
{
    $user = $this->user; // 2. instantiate the variable we created in our construct above

    //create group list array
    $groupList = $this->user->getACRGroups();
    $gla = array();
    foreach ($groupList as $g) {
        $gla[] = $g->getId();
    };

    $builder
    ->add('acr_group', EntityType::class, array(
        'label' => 'ATS',
        'class' => 'HazardlogBundle:ACRGroup',
        'query_builder' => function (EntityRepository $er) use ($gla) { // 3. use the user variable in the querybilder
                $qb = $er->createQueryBuilder('g');
                $qb->where('g.id IN (:gla)');
                $qb->setParameters( array('gla' => $gla) );
                $qb->orderBy('g.name', 'ASC');
                return $qb;
        },
        'choice_label' => 'name'
    ))

1
2
3
4
$q = $this->createQueryBuilder('v')
    ->select('v')
    ->andWhere('v.workingHours IN (:workingHours)')
    ->setParameter('workingHours', $workingHours);

From:使用实体集合的Doctrine 2 WHERE IN子句

或根据原则文档:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html#the-expr-class
若要在带有条件的查询生成器中插入IN条件,可以使用expr()

1
2
3
4
5
6
7
$qb->add('select', new Expr\\Select(array('u')))
   ->add('from', new Expr\\From('User', 'u'))
   ->add('where', $qb->expr()->orX(
       $qb->expr()->eq('u.id', '?1'),
       $qb->expr()->like('u.nickname', '?2')
   ))
   ->add('orderBy', new Expr\\OrderBy('u.name', 'ASC'));

IN的语法:

1
$qb->expr()->in('u.id', array(1, 2, 3))

此外,请确保不要使用与$qb->expr()->in('value', array('stringvalue'))类似的东西,因为这将导致Doctrine抛出异常。而是使用$qb->expr()->in('value', array('?1'))并将您的参数绑定到?1