具有附加聚合字段的 Doctrine 实体存储库
Doctrine entity repository with additional aggregated fields
我有一个实体 Client
,它与一个实体 Contract
有关系。合同有一个字段 amount
和一个字段 payingDelay
.
Client.php
/**
* @ORM\OneToMany(targetEntity="Contract", mappedBy="client")
* @ORM\JoinColumn(name="contract_id", referencedColumnName="id")
*/
private $contract;
我想显示所有客户的列表,其中包含一些基本的客户字段以及一些计算的(SUM 等)合同信息,如下所示:
name - num contracts - sum(amounts) - aggregated risk
John - COUNT(contracts) - SUM(C.amount) - SUM(C.amount * C.payingDelay)
This is my basic `findClientWithCalculations()` method in `ClientRepository`:
return $this->createQueryBuilder('CLI')
->join('CLI.contract', 'CON')
->orderBy('CON.startDate', 'DESC')
->getQuery()
->getResult();
Is there a way I can add extra columns to this QueryBuilder, even if the final structure doesn't match the structure of a Client object or this must be done outside from a repository?
If not, maybe I can build a custom query in a controller and pass the query result to a twig template to show this structure.
Thank you.
虽然不是微不足道的问题,但问题的表述不正确。我认为实体存储库方法必须始终实现某种 findBy()
方法和 return 该存储库所属实体的对象或对象集合。
实际上,实体存储库方法可以return任何东西,因此可以使用实体存储库方法内部的本机查询来解决这个问题。
例如:
ClientRepository.php:
public function findWithContractStatus($contractStatusShortname)
{
$em = $this->getEntityManager();
$clientQuery = "select distinct CLI.id, CLI.name, COUNT(contracts) as ncontracts, SUM(C.amount) as amount from client CLI join contract CON on CON.client_id = CON.id group by CLI.id, CLI.name"
$rsm = new ResultSetMapping();
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('name', 'name');
$rsm->addScalarResult('ncontracts', 'ncontracts');
$rsm->addScalarResult('amount', 'amount');
$query = $em->createNativeQuery($clientQuery, $rsm);
return $query->getResult();
}
这将 return 一个具有给定结构的数组 - id、name、ncontracts、amount - 可以在控制器、twig 模板或任何地方迭代。
我有一个实体 Client
,它与一个实体 Contract
有关系。合同有一个字段 amount
和一个字段 payingDelay
.
Client.php
/**
* @ORM\OneToMany(targetEntity="Contract", mappedBy="client")
* @ORM\JoinColumn(name="contract_id", referencedColumnName="id")
*/
private $contract;
我想显示所有客户的列表,其中包含一些基本的客户字段以及一些计算的(SUM 等)合同信息,如下所示:
name - num contracts - sum(amounts) - aggregated risk
John - COUNT(contracts) - SUM(C.amount) - SUM(C.amount * C.payingDelay)
This is my basic `findClientWithCalculations()` method in `ClientRepository`:
return $this->createQueryBuilder('CLI')
->join('CLI.contract', 'CON')
->orderBy('CON.startDate', 'DESC')
->getQuery()
->getResult();
Is there a way I can add extra columns to this QueryBuilder, even if the final structure doesn't match the structure of a Client object or this must be done outside from a repository?
If not, maybe I can build a custom query in a controller and pass the query result to a twig template to show this structure.
Thank you.
虽然不是微不足道的问题,但问题的表述不正确。我认为实体存储库方法必须始终实现某种 findBy()
方法和 return 该存储库所属实体的对象或对象集合。
实际上,实体存储库方法可以return任何东西,因此可以使用实体存储库方法内部的本机查询来解决这个问题。
例如:
ClientRepository.php:
public function findWithContractStatus($contractStatusShortname)
{
$em = $this->getEntityManager();
$clientQuery = "select distinct CLI.id, CLI.name, COUNT(contracts) as ncontracts, SUM(C.amount) as amount from client CLI join contract CON on CON.client_id = CON.id group by CLI.id, CLI.name"
$rsm = new ResultSetMapping();
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('name', 'name');
$rsm->addScalarResult('ncontracts', 'ncontracts');
$rsm->addScalarResult('amount', 'amount');
$query = $em->createNativeQuery($clientQuery, $rsm);
return $query->getResult();
}
这将 return 一个具有给定结构的数组 - id、name、ncontracts、amount - 可以在控制器、twig 模板或任何地方迭代。