2014-05-21 2 views
0

Doctrine 2를 사용하여 엔티티에서 부모를 가져옵니다. 다음 엔티티 모델이 있습니다 :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); 

답변

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(); 
:

나는이 쿼리를 가지고있다