Doctrine 2 WHERE IN子句使用实体集合 [英] Doctrine 2 WHERE IN clause using a collection of entities
问题描述
我试图在Doctrine 2中构建一个查询,查找与任何给定的 VacancyWorkingHours $相关的所有
空缺
实体c $ c>实体。
空缺
实体如下:
/ **
*空缺
*
* @ ORM\Table(name =vacancy)
* @ ORM\Entity(repositoryClass =JaikDean\CareersBundle\Entity\VacancyRepository)
* /
class空缺
{
/ **
* @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;
/ *其他字段和方法无关紧要* /
}
我的查询目前看起来如下,但由于where子句不会返回任何结果。在这个例子中, $ workingHours
是一个 Doctrine\Common\Collections\ArrayCollection
实例,包含一些 VacancyWorkingHours
实体
$ q = $ this-> createQueryBuilder('v ')
- > select('v')
- > andWhere('v.workingHours IN(:workingHours)')
- > setParameter('workingHours',$ workingHours - >指定者());
;
A 拉请求我做的这个已经被合并到Doctrine ORM 2.5中,所以你现在可以这样做:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ v v v v v v v v v v v v v v v v v v v v v .workingHours IN(:workingHours)')
- > setParameter('workingHours',$ workingHours);
;
最新版本的Doctrine现在允许收集参数,并将自动使用每个的主键收集条目。
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.
The Vacancy
entity looks as follows:
/**
* 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
$q = $this->createQueryBuilder('v')
->select('v')
->andWhere('v.workingHours IN (:workingHours)')
->setParameter('workingHours', $workingHours->toArray());
;
A pull request I made about this has been merged into Doctrine ORM 2.5, so you can simply do this now:
$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 2 WHERE IN子句使用实体集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!