1
我有4個實體:Country,Region,Province,Town。使用DQL與Doctrine2實體和Zend框架混淆
<?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
感謝。
您好,感謝您的觀點。我確實有getter和setter,但是把它們排除在外以使代碼更短。 – dimbo 2012-01-16 17:49:41
嗨,我一直試圖做LEFT加入整天,並不斷提出反對這個錯誤:「錯誤:類實體\鎮有沒有關聯名爲省」。我已經嘗試了上面的代碼(不包括WHERE條件),並且我得到了同樣的錯誤。有任何想法嗎? – dimbo 2012-01-16 17:52:32
您應該重新命名私有$ provinces_id;私人$省;在你的城鎮實體,這應該做的伎倆! – 2012-01-16 20:25:27