2015-02-09 42 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