我有一个相当简单的产品数据库,其中品牌有很多产品和颜色,产品有一种或多种颜色。Docrtine多对多数不计数BY
我想选择一个品牌实体的所有颜色以及在单个查询中使用该特定颜色的所有产品的计数,否则我将以30个以上的查询结束页面加载!
主义实体
品牌
class Brand
{
/**
* @ORM\Id
* @ORM\Column(type="integer", options={"unsigned"=true})
* @ORM\GeneratedValue
*/
protected $brandId;
/**
* @ORM\Column(type="string", length=45, unique=true)
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="brand")
*/
protected $products;
/**
* @ORM\OneToMany(targetEntity="Colour", mappedBy="brand")
*/
protected $colours;
}
产品
class Product
{
/**
* @ORM\Id
* @ORM\Column(type="integer", options={"unsigned"=true})
* @ORM\GeneratedValue
*/
protected $productId;
/**
* @ORM\Column(type="string", length=45)
*/
protected $name;
/**
* @ORM\ManyToOne(targetEntity="Brand", inversedBy="products")
* @ORM\JoinColumn(name="brandId", referencedColumnName="brandId", nullable=false)
*/
protected $brand;
/**
* @ORM\ManyToMany(targetEntity="Colour")
* @ORM\JoinTable(name="productColours",
* joinColumns={@ORM\JoinColumn(name="productId", referencedColumnName="productId")},
* inverseJoinColumns={@ORM\JoinColumn(name="colourId", referencedColumnName="colourId")}
*)
*/
protected $colours;
}
颜色
class Colour
{
/**
* @ORM\Id
* @ORM\Column(type="integer", options={"unsigned"=true})
* @ORM\GeneratedValue
*/
protected $colourId;
/**
* @ORM\Column(type="string", length=20)
*/
protected $name;
/**
* @ORM\ManyToOne(targetEntity="Brand", inversedBy="colours")
* @ORM\JoinColumn(name="brandId", referencedColumnName="brandId", nullable=false)
*/
protected $brand;
}
这普拉伊ñSQL这是很容易...
SELECT c.*, b.*, count(p.productId) as productCount
FROM colours c
INNER JOIN brands b ON c.brandId = b.brandId
LEFT JOIN productColours pc ON pc.colourId = c.colourId
LEFT JOIN products p ON p.productId = pc.productId
GROUP BY c.colourId
教义我无法找到解决的办法不增加双向映射到我真的不只是一个产品的计数想要的颜色。
我目前的查询看起来像这样(它不作为没有c.products映射工作)。
$query = $this->getEntityManager()->createQueryBuilder()
->select(
'c AS colour',
'b AS brand',
'COUNT(DISTINCT p.productId) AS productCount'
)
->from('Colour', 'c')
->innerJoin('c.brand', 'b')
->leftJoin('c.products', 'p');
而且假设上述不会映射品牌进入颜色实体,所以我不能去$colour->getBrand()
?
为什么你不喜欢双向关联?这可能是你关心的表现吗? – Mark
在整个项目中,我会在很多场合想要完成同样的任务。似乎疯狂做双向计数。 – Nick
如果映射正确,双向关联不会做错。请参阅下面的答案。 – Mark