检索实体集合上的非重复计数
的最佳实践是什么?
在这个示例实体(customer
)中,我与orders
有一个OneTomany
关系。
我想数一下有多少销售& 客户订购的产品:
> select * from orders;
+----------+----------+----------+
| customer | sale_ref | prod_ref |
+----------+----------+----------+
| 1 | sale_1 | prod_1 |
| 1 | sale_1 | prod_2 |
| 1 | sale_2 | prod_1 |
| 1 | sale_3 | prod_3 |
+----------+----------+----------+
> select count(prod_ref) from order where customer = 1;
+-----------------+
| count(prod_ref) |
+-----------------+
| 4 |
+-----------------+
> select count(distinct(sale_ref)) from order where customer = 1;
+-----------------+
| count(prod_ref) |
+-----------------+
| 3 |
+-----------------+
这是代码
use Doctrine\ORM\Mapping as ORM;
class Customer
{
/**
* @var \Doctrine\Common\Collections\Collection
* @ORM\OneToMany(targetEntity="Orders", mappedBy="customer", cascade={"persist", "remove"}, fetch="EXTRA_LAZY")
*/
protected $orders;
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getOrders(): \Doctrine\Common\Collections\Collection
{
return $this->orders;
}
/**
* @return int
*/
public function getOrdersProductCount(): int
{
return $this->orders->count();
}
}
class Orders
{
/**
* @var Customer $customer
* @ORM\ManyToOne(targetEntity="Customer", inversedBy="orders")
*/
protected $customer;
/**
* Non-unique sales reference
* @var string $salesRef
* @ORM\Column(name="sales_ref", type="string")
*/
protected $salesRef;
/**
* Unique product reference
* @var string $productRef
* @ORM\Column(name="product_ref", type="string")
*/
protected $productRef;
/**
* @return Customer
*/
public function getCustomer(): Customer
{
return $this->customer;
}
/**
* @return string
*/
public function getProductRef(): string
{
return $this->productRef;
}
/**
* @return string
*/
public function getSalesRef(): string
{
return $this->salesRef;
}
}
使用customer->getOrdersProductCount()
可以很好地检索产品计数,并且被认为是“良好的实践”,因为它不会在完整加载集合时访问数据库:
https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/tutorials/extra-lazy-associations.html
如果将关联标记为额外惰性,则可以调用集合上的下列方法,而无需触发集合collection#count()
的完整加载
但是,在本例中,一个customer
可以有多个销售产品-其中salesref
是非唯一的。 检索salesref
的非重复计数
的最佳方法是什么?
这可以/应该在实体存储库类中处理:
class OrdersRepository
{
public function getSalesCount($customer): int
{
return (int)$this->createQueryBuilder('o')
->select('COUNT(DISTINCT(o.salesRef))')
->where('o.customer = :customer')
->setParameter('customer', $customer)
->setMaxResults(1)
->getQuery()
->getSingleScalarResult();
}
public function getProductCount($customer): int
{
return (int)$this->createQueryBuilder('o')
->select('COUNT(o.productRef)')
->where('o.customer = :customer')
->setParameter('customer', $customer)
->setMaxResults(1)
->getQuery()
->getSingleScalarResult();
}
}
这是可行的,但我需要加载EntityManager/CustomerRepository才能访问这些方法--而至少我可以从实体中检索产品计数。。。。
我如何才能从客户实体中访问不同的销售计数--如果有的话?
我考虑过使用collection#filter()
方法和/或循环访问orders
实体来创建一个以salesref
为键的数组,然后使用array_unique#count()
,但这似乎并不“正确”--我想我知道答案(使用实体存储库),但我更希望能够从customer
实体中访问销售计数--最佳实践/方法是什么?
我认为这应该做到这一点,而且这将是一种更便携的方式。 但我没有测试它。
$qb = $this->createQueryBuilder('o');
return (int)$qb
->select($qb->expr()->countDistinct('o.salesRef'))
->where('o.customer = :customer')
->setParameter('o.customer', $customer)
->setMaxResults(1)
->getQuery()
->getSingleScalarResult();
参考见:https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/Reference/query-builder.html#the-querybuilder
希望这有帮助
您可以将一个未映射的字段添加到您的实体中,并创建一个侦听器来侦听Doctory postLoad事件。 在这个侦听器中,您可以注入存储库并手动设置查询的结果。