doctrine queryBuilder where IN 集合
doctrine queryBuilder where IN collection
在我的实体上,我有一个用户数组集合
/**
* @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() 的参数?
$q = $this->createQueryBuilder('v')
->select('v')
->andWhere('v.workingHours IN (:workingHours)')
->setParameter('workingHours', $workingHours);
发件人:Doctrine 2 WHERE IN clause using a collection of entities
或者根据学说文档:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html#the-expr-class
要使用 doctrine 在查询构建器中插入 IN 条件,您可以使用 expr()
$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 的语法:
$qb->expr()->in('u.id', array(1, 2, 3))
此外,请确保您不使用类似于 $qb->expr()->in('value', array('stringvalue'))
的东西,因为这会导致 Doctrine 抛出异常。相反,使用 $qb->expr()->in('value', array('?1'))
并将您的参数绑定到 ?1
试试下面的代码
$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()
来获取特定的列。
在尝试您的一些解决方案失败后,我最终扭转了局面。我手动创建了一个我想要的 ID 数组。
可能有一种本机的方法可以做到这一点,这似乎是一种非常标准的方法……但是这很有效。
// 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'
))
在我的实体上,我有一个用户数组集合
/**
* @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() 的参数?
$q = $this->createQueryBuilder('v')
->select('v')
->andWhere('v.workingHours IN (:workingHours)')
->setParameter('workingHours', $workingHours);
发件人:Doctrine 2 WHERE IN clause using a collection of entities
或者根据学说文档:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html#the-expr-class 要使用 doctrine 在查询构建器中插入 IN 条件,您可以使用 expr()
$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 的语法:
$qb->expr()->in('u.id', array(1, 2, 3))
此外,请确保您不使用类似于 $qb->expr()->in('value', array('stringvalue'))
的东西,因为这会导致 Doctrine 抛出异常。相反,使用 $qb->expr()->in('value', array('?1'))
并将您的参数绑定到 ?1
试试下面的代码
$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()
来获取特定的列。
在尝试您的一些解决方案失败后,我最终扭转了局面。我手动创建了一个我想要的 ID 数组。
可能有一种本机的方法可以做到这一点,这似乎是一种非常标准的方法……但是这很有效。
// 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'
))