php Doctrine 2 WHERE IN 子句使用实体集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18536190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Doctrine 2 WHERE IN clause using a collection of entities
提问by Jaik Dean
I'm attempting to build a query in Doctrine 2 that finds all Vacancy
entities which are related to any of the given VacancyWorkingHours
entities.
我正在尝试在 Doctrine 2 中构建一个查询,以查找Vacancy
与任何给定VacancyWorkingHours
实体相关的所有实体。
The Vacancy
entity looks as follows:
该Vacancy
实体如下所示:
/**
* Vacancy
*
* @ORM\Table(name="vacancy")
* @ORM\Entity(repositoryClass="JaikDean\CareersBundle\Entity\VacancyRepository")
*/
class Vacancy
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var VacancyWorkingHours
*
* @ORM\ManyToOne(targetEntity="VacancyWorkingHours", inversedBy="vacancies")
* @ORM\JoinColumn(name="vacancy_working_hours_id", referencedColumnName="id")
**/
private $workingHours;
/* Other fields and methods are inconsequential */
}
My query currently looks as follows, but returns no results because of the where clause. In this example, $workingHours
is a Doctrine\Common\Collections\ArrayCollection
instance containing a number of VacancyWorkingHours
entities
我的查询目前如下所示,但由于 where 子句,没有返回任何结果。在这个例子中,$workingHours
是一个Doctrine\Common\Collections\ArrayCollection
包含许多VacancyWorkingHours
实体的实例
$q = $this->createQueryBuilder('v')
->select('v')
->andWhere('v.workingHours IN (:workingHours)')
->setParameter('workingHours', $workingHours->toArray());
;
回答by Micha?l Perrin
A pull requestI made about this has been merged into Doctrine ORM 2.5, so you can simply do this now:
我就此提出的拉取请求已合并到 Doctrine ORM 2.5 中,因此您现在可以简单地执行以下操作:
$q = $this->createQueryBuilder('v')
->select('v')
->andWhere('v.workingHours IN (:workingHours)')
->setParameter('workingHours', $workingHours);
;
The latest version of Doctrine now allows collection parameters and will automatically make use of the primary key of each of the collection entries.
最新版本的 Doctrine 现在允许使用集合参数,并将自动使用每个集合条目的主键。
回答by Alexey B.
Try to set IDs as parameter
尝试将 ID 设置为参数
$ids = array();
foreach($workingHours as $w) {
$ids[] = $w->getId();
}
Then
然后
$q = $this->createQueryBuilder('v')
->select('v')
->andWhere('v.workingHours IN (:workingHours)')
->setParameter('workingHours', $ids);
;
回答by ybert
I think the DQL will work better for that.
我认为 DQL 会为此工作得更好。
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery(
'SELECT v
FROM YourAppYourBundle:YourEntity v // exemple AcmeexampleBundle:YourEntity
WHERE v.workingHours IN :workingHours'
)->setParameter('workingHours', $workingHours->toArray());
$vacancies = $query->getResult();
回答by MAZux
I suggest using DQL in this way:
我建议以这种方式使用 DQL:
$qb = $this->createQueryBuilder('v')
->andWhere($qb->expr()->in('v.workingHours', $ids));
;
And let Doctrine handle the expression & quotation work for you.
让 Doctrine 为您处理表达式和引用工作。