1

我有 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

谢谢。

4

1 回答 1

3

先说几件事。

  1. 不要用 命名您的字段_id,因为它们不是标识符,而是与其他对象的关系。连接列注释带有真实的数据库名称,对象模型中的字段不带。
  2. 为所有字段编写/生成 get/set/add 方法以封装它们,因此您可以实际使用它们。您无法从“外部”读取私有字段。

至于你的问题,还没有测试过,但是这样的东西应该可以工作。

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');
}
于 2012-01-16T17:23:52.800 回答