Doctrine 2递归查询构建器

时间:2014-05-21 04:17:46

标签: php symfony doctrine-orm doctrine

我正在使用Doctrine 2来获取实体的父母。我有下一个实体模型:

<?php

namespace Core\Base\Model\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="modules", options={"comment" = "modules installed"});
 */
class Module
{

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=150, options={"comment" = "module\'s name"}) 
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=1500, options={"comment" = "module\'s   description"}) 
     */
    private $description;

    /**
     * @ORM\OneToMany(targetEntity="Core\Base\Model\Entity\Module", mappedBy="parentModule")
     */
    private $subModules;

    /**
     * @ORM\ManyToOne(targetEntity="Core\Base\Model\Entity\Module", inversedBy="subModules")
     * @ORM\JoinColumn(name="module_parent_id", referencedColumnName="id")
     */
    private $parentModule;

    public function __construct()
    {
        $this->subModules = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * @return int Module's id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return String Module's name
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return String Module's description
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @return \Doctrine\Common\Collections\ArrayCollection
     */
    public function getSubModules()
    {
        return $this->subModules;
    }

    /**
     * @param int $id Module's identificator
     * @return void
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @param String $name Modul'es name
     * @return void
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @param String $description Module's description
     * @return void
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /**
     * @param \Core\Module $module
     */
    public function addSubModule($module)
    {
        $this->subModules->add($module);
    }

    /**
     * @param \Core\Module $module
     */
    public function addParentModule($module)
    {
        $this->parentModule = $module;
    }
}

我想得到所有的父母。换句话说,parentModule中的所有模块都为null。我怎么能这样做?

我有这个问题:

$dql = $this->entityManager->createQueryBuilder()
                ->select('m')
                ->from('\Core\Base\Model\Entity\Module', 'm');

        return $this->getArrayResult($dql);

1 个答案:

答案 0 :(得分:0)

$qb = $this->entityManager->createQueryBuilder();

$qb
    ->select('m')
    ->from('\Core\Base\Model\Entity\Module', 'm')
    ->where(
        $qb->expr()->isNull('m.parentModule')
    );

return $this->getQuery->getResult();