假设我有3个实体:
托盘,卡片,卡片类型
托盘有卡片,每张卡片都有卡片颜色。
使用doctrine2可以为查询添加每个的计数吗?
e.g。
$qb = $this->getDoctrine()->getManager()->getQueryBuilder();
$query = $qb->select('t',$qb->expr()->count('blueCards'))->from('Tray')
->leftJoin('t.Cards','blueCards')
->andWhere('bluecards.CardColour.col = :col')
->setParameter('col','blue');
然后还会获得黄牌和红牌的数量。
例如最终加载:
Tray.name: tray1
Tray.blueCardsCount = 10
Tray.yellowCardsCount = 12
Tray.redCardsCount = 3
我希望这个解释得很好......
答案 0 :(得分:1)
您可以合并这样的计数(更新一下以匹配您的实际查询代码):
$query = $this->getDoctrine()->getManager()->getRepository('[YourBundle]:Tray')->createQueryBuilder('tray')
->select('tray.name, count(blueCard) as blueCardsCount, count(yellowCard) as yellowCardsCount, count(redCard) as redCardsCount')
->leftJoin('tray.Cards', 'card')
->leftJoin('card.CardColour', 'blueCard', 'WITH', 'blueCard.col = "blue"')
->leftJoin('card.CardColour', 'redCard', 'WITH', 'redCard.col = "red"')
->leftJoin('card.CardColour', 'yellowCard', 'WITH', 'yellowCard.col = "yellow"')
->groupBy('tray.name') // or id...
->getQuery();
$result = $query->getResult();
请注意,这会返回数组中的值(而不是实体)。
答案 1 :(得分:1)
我想你可以在这里工作! Query::HYDRATE_SCALAR
返回一个数组。 this
<强>纸盘强>
/**
* @ORM\Entity(repositoryClass="Your\WhatBundle\Repository\TrayRepository")
* @ORM\Table(name="tray")
*/
class Tray
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(
* targetEntity="Card",
* mappedBy="tray",
* cascade={"persist", "remove"}
* )
*/
protected $card;
}
<强>卡强>
/**
* @ORM\Entity
* @ORM\Table(name="card")
*/
class Card
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(
* targetEntity="Type",
* mappedBy="card",
* cascade={"persist", "remove"}
* )
*/
protected $type;
/**
* @ORM\ManyToOne(
* targetEntity="Tray",
* inversedBy="card"
* )
* @ORM\JoinColumn(
* name="tray_id",
* referencedColumnName="id",
* onDelete="CASCADE",
* nullable=false
* )
*/
protected $tray;
}
<强>类型强>
/**
* @ORM\Entity
* @ORM\Table(name="type")
*/
class Type
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(
* targetEntity="Card",
* inversedBy="type"
* )
* @ORM\JoinColumn(
* name="card_id",
* referencedColumnName="id",
* onDelete="CASCADE",
* nullable=false
* )
*/
protected $card;
}
<强>存储库强>
class TrayRepository extends EntityRepository
{
public function getCounts()
{
return
$this
->createQueryBuilder('tr')
->select('COUNT(tr) AS TR, COUNT(tr) AS CR, COUNT(ty) AS TY')
->leftJoin('tr.card', 'cr')
->leftJoin('cr.type', 'ty')
->getQuery()
->getResult(Query::HYDRATE_SCALAR);
}
}
<强>控制器强>
class DefaultController extends Controller
{
/**
* @Route("")
* @Method({"GET"})
*/
public function indexAction()
{
$repo = $this->getDoctrine()->getRepository('YourWhatBundle:Tray');
$result = $repo->getCounts();
echo '<pre>';
print_r($result);
}
}
<强>结果强>
Array
(
[0] => Array
(
[TR] => 5
[CR] => 5
[TY] => 3
)
)