“Doctrine Query”构建器,计数相关的一对多行

时间:2015-02-09 12:28:28

标签: php sql symfony doctrine-orm

<?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;
    }
}

My 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 个答案:

答案 0 :(得分:8)

总结评论:

$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