我有4个实体:国家,地区,省,镇。
<?php
namespace Entities;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity (repositoryClass="Repositories\Region")
* @Table(name="regions")
* @HasLifecycleCallbacks
*/
class Region {
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/** @Column(type="string", length=30,unique=TRUE) */
private $regionname;
/** @Column(type="boolean") */
private $active;
/**
* @ManyToOne(targetEntity="Country", inversedBy="regions")
* @JoinColumn(name="countries_id", referencedColumnName="id",nullable=FALSE)
*/
private $countries_id;
/**
* @OneToMany(targetEntity="Province", mappedBy="provinces")
*/
private $provinces;
public function __construct() {
$this->provinces = new ArrayCollection();
$this->active = true;
}
<?php
namespace Entities;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity (repositoryClass="Repositories\Province")
* @Table(name="provinces")
* @HasLifecycleCallbacks
*/
class Province {
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/** @Column(type="string", length=30,unique=TRUE) */
private $provincename;
/** @Column(type="boolean") */
private $active;
/**
* @ManyToOne(targetEntity="Region", inversedBy="provinces")
* @JoinColumn(name="regions_id", referencedColumnName="id",nullable=FALSE)
*/
private $regions_id;
/**
* @OneToMany(targetEntity="Town", mappedBy="towns")
*/
private $towns;
public function __construct() {
$this->towns = new ArrayCollection();
$this->active = true;
}
<?php
namespace Entities;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity (repositoryClass="Repositories\Town")
* @Table(name="towns")
* @HasLifecycleCallbacks
*/
class Town {
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/** @Column(type="string", length=30,unique=FALSE) */
private $townname;
/** @Column(type="boolean") */
private $active;
// so that we know when a user has added a town
/** @Column(type="boolean") */
private $verified;
/**
* @OneToMany(targetEntity="User", mappedBy="users")
*/
private $users;
/**
* @ManyToOne(targetEntity="Province", inversedBy="towns")
* @JoinColumn(name="provinces_id", referencedColumnName="id",nullable=FALSE)
*/
private $provinces_id;
public function __construct() {
$this->users = new ArrayCollection();
$this->active = true;
}
我想使用DQL创建一个查询,它将为我提供给定区域的城镇列表。
获取我正在使用的活跃城镇的简单列表:
public function findActiveTowns($provinces_id = null)
// we can pass in a specific provinces_id if we want
{
$qb = $this->_em->createQueryBuilder();
$qb->select('a.townname, a.id')
->from('Entities\Town', 'a');
if (!is_null($provinces_id)){
$qb->where('a.provinces_id = :provinces_id AND a.active = TRUE')
->setParameter('provinces_id', $provinces_id);
} else {
$qb->where('a.active = TRUE');
}
$towns=$qb->getQuery()->getResult();
// make pairs array suitable for select lists
$options = array();
foreach ($towns as $key => $value) {
$options[$value['id']] = $value['townname'];
}
return $options;
}
现在,要明白这一点。如何设置连接并使其正常工作,以便我们可以传入region_id并返回该区域中的所有城镇。
在本机SQL中我会做这样的事情:
SELECT towns.id
FROM `towns`
INNER JOIN `provinces`
INNER JOIN `regions`
WHERE regions.id =1
感谢。
答案 0 :(得分:3)
首先要做的一些事情。
_id
为您的字段命名,因为它们不是标识符,而是与其他对象的关系。连接列注释与真实数据库名称一致,对象模型中的字段不用。至于你的问题,还没有测试过,但这样的事情应该有效。
class Town {
/**
* @ManyToOne(targetEntity="Province", inversedBy="towns")
* @JoinColumn(name="provinces_id", referencedColumnName="id",nullable=FALSE)
*/
private $province;
class Province {
/**
* @ManyToOne(targetEntity="Region", inversedBy="provinces")
* @JoinColumn(name="regions_id", referencedColumnName="id",nullable=FALSE)
*/
private $region;
$qb->select('a.townname, a.id')
->from('Entities\Town', 'a')
->leftJoin('a.province', 'p');
if (!is_null($provinces_id) && !is_null($region_id)){
$qb->where('a.province = :province AND a.active = TRUE')
->andWhere('p.region = :region')
->setParameter('province', $provinces_id)
->setParameter('region', $region_id);
} else {
$qb->where('a.active = TRUE');
}