2015-02-09 39 views
4
<?php 
namespace Raltech\WarehouseBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* @ORM\Entity 
* @ORM\Table(name="warehouse_magazine") 
*/ 
class Magazine 
{ 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\Column(type="string", length=100) 
    */ 
    protected $name; 


    /** 
    * @ORM\Column(type="text") 
    */ 
    protected $description; 


    /** 
    * @ORM\OneToMany(targetEntity="Wardrobe", mappedBy="magazine",cascade={"remove"}) 
    */ 
    protected $wardrobe; 


    public function __construct() 
    { 
     $this->wardrobe = new ArrayCollection(); 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * @return Magazine 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Set description 
    * 
    * @param string $description 
    * @return Magazine 
    */ 
    public function setDescription($description) 
    { 
     $this->description = $description; 

     return $this; 
    } 

    /** 
    * Get description 
    * 
    * @return string 
    */ 
    public function getDescription() 
    { 
     return $this->description; 
    } 

    /** 
    * Add wardrobe 
    * 
    * @param \Raltech\WarehouseBundle\Entity\Wardrobe $wardrobe 
    * @return Magazine 
    */ 
    public function addWardrobe(\Raltech\WarehouseBundle\Entity\Wardrobe $wardrobe) 
    { 
     $this->wardrobe[] = $wardrobe; 

     return $this; 
    } 

    /** 
    * Remove wardrobe 
    * 
    * @param \Raltech\WarehouseBundle\Entity\Wardrobe $wardrobe 
    */ 
    public function removeWardrobe(\Raltech\WarehouseBundle\Entity\Wardrobe $wardrobe) 
    { 
     $this->wardrobe->removeElement($wardrobe); 
    } 

    /** 
    * Get wardrobe 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getWardrobe() 
    { 
     return $this->wardrobe; 
    } 
} 


<?php 
namespace Raltech\WarehouseBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="warehouse_wardrobe") 
*/ 
class Wardrobe 
{ 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\Column(type="string", length=100) 
    */ 
    protected $name; 


    /** 
    * @ORM\Column(type="text") 
    */ 
    protected $description; 


    /** 
    * @ORM\ManyToOne(targetEntity="Magazine", inversedBy="wardrobe") 
    * @ORM\JoinColumn(name="magazine_id", referencedColumnName="id") 
    */ 
    protected $magazine; 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * @return Wardrobe 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Set description 
    * 
    * @param string $description 
    * @return Wardrobe 
    */ 
    public function setDescription($description) 
    { 
     $this->description = $description; 

     return $this; 
    } 

    /** 
    * Get description 
    * 
    * @return string 
    */ 
    public function getDescription() 
    { 
     return $this->description; 
    } 

    /** 
    * Set magazine 
    * 
    * @param \Raltech\WarehouseBundle\Entity\Magazine $magazine 
    * @return Wardrobe 
    */ 
    public function setMagazine(\Raltech\WarehouseBundle\Entity\Magazine $magazine = null) 
    { 
     $this->magazine = $magazine; 

     return $this; 
    } 

    /** 
    * Get magazine 
    * 
    * @return \Raltech\WarehouseBundle\Entity\Magazine 
    */ 
    public function getMagazine() 
    { 
     return $this->magazine; 
    } 
} 

我2個entites的,我想指望每個雜誌是多少衣櫃相關的,我必須從QueryBuilder的主義查詢生成器,算上相關的一對多行

$em = $this->get('doctrine.orm.entity_manager'); 
$userRepository = $em->getRepository('Raltech\WarehouseBundle\Entity\Magazine'); 
$qb = $userRepository->createQueryBuilder('magazine') 
    ->addSelect("magazine.id,magazine.name,magazine.description") 
    ->InnerJoin('magazine.wardrobe', 'wardrobe') 
    ->addSelect('COUNT(wardrobe.id) AS wardrobecount') 

這不起作用使這個當然。 那麼,有人可以提供示例如何從querybuilder中計數?

+1

加入' - > GROUPBY( 'magazine.id')'? – 2015-02-09 14:05:45

+1

我認爲它應該是'innerJoin',小寫字母「i」。 – 2015-02-09 14:11:18

+0

它的工作原理,但我怎麼能顯示不相關(wardrobecount == 0)? – user3468055 2015-02-09 14:16:41

回答

7

總結意見:

$qb = $userRepository->createQueryBuilder('magazine') 
->addSelect("magazine.id,magazine.name,magazine.description") 
->leftJoin('magazine.wardrobe', 'wardrobe') // To show as well the magazines without wardrobes related 
->addSelect('COUNT(wardrobe.id) AS wardrobecount') 
->groupBy('magazine.id'); // To group the results per magazine 
+0

當我想要計算兩個不同的關係時,如wardrobecount和toiletriescount?會發生什麼? – afilina 2015-09-02 20:25:32

+1

你應該可以做另一個左連接' - > leftJoin('magazine.toletries','toiletries')',然後在另一列上選擇count - > addSelect('COUNT(toletries.id)AS toiletriescount') '。不過,根據您的需要,您可能希望執行' - > addSelect('COUNT(distinct toletries.id)AS toiletriescount')',因爲它是左連接。 – 2015-09-03 10:53:35

+0

我比較關心這個小組。我想我的例子並不能說明問題。假設你正在計算與雜誌以外的內容有關的東西。我基本上會建議使用子查詢。一個小組不夠靈活。在這種特殊情況下工作良好,但不會擴展。 – afilina 2015-09-03 17:32:36