我有一个存储库,它有两个函数可以返回3个相关实体的值。 Vehicle - OneToMany - Jobs,Vehivle - OneToMany - FuelPurchase
在车辆中我有以下工作资料库:
class VehicleRepository extends EntityRepository
{
public function findByFuelXVehicle ($dateStart = null, $dateEnd=null)
{
$query = $this->createQueryBuilder('v')
->select('v.plateNumber',
'SUM(f.fuelUsed) as totalFuel',
'SUM(f.fuelUsed*f.fuelPrice) as totalFuelCost')
->join('v.fuelPurchaces', 'f')
->groupBy('v.plateNumber')
->getQuery()->getResult();
return $query;
}
public function findByJobXVehicle($dateStart = null, $dateEnd=null){
$query = $this->createQueryBuilder('v')
->select('v.plateNumber','SUM(j.kmOdoEnd - j.kmOdoStart)')
->join('v.jobs', 'j')
->groupBy('v.plateNumber');
$q = $query->getQuery()->getResult();
return $q;
}
}
如何将这两个函数的结果仅以一种方式集成,以便我可以在这些函数中为idVehicle
所表示的v
集成的查询提供结果。像这样的东西:
我已经尝试了一个通过数组的功能,但我可以设法让它工作。
public function findByIntegrated ($dateStart = null, $dateEnd=null)
{
$fuels = $this->findByFuelXVehicle($dateStart = null, $dateEnd=null);
$jobs = $this->findByJobXVehicle($dateStart = null, $dateEnd=null);
$jobAndFuel = null;
foreach ($jobs as $key => $job){
$jobAndFuel[$key]['plateNumber']=$job['plateNumber'];
$jobAndFuel[$key]['totalDistance']=$job['totalDistance'];
$jobAndFuel[$key]['countJob']=$job['countJob'];
}
for ($i =0;$i<count($fuels);$i++){
for($k=0;$k<count($jobAndFuel);$k++){
if ($jobAndFuel[$k]['plateNumber'] == $fuels[$i]['plateNumber']){
$jobAndFuel[$k]['totalFuel']=$fuels[$i]['totalFuel'];
$jobAndFuel[$k]['totalFuelCost']=$fuels[$i]['totalFuelCost'];
}
}
}
return $jobAndFuel;
}
}
问题在于,当不是所有汽车都有燃料日志或工作时。
答案 0 :(得分:0)
它导致比我想象的要复杂一些,但我设法使用来自这里和那里的信息使其工作。
<?php
namespace TeamERP\TransportBundle\Entity;
use Doctrine\ORM\EntityRepository;
class VehicleRepository extends EntityRepository
{
/* Find vehicle fuel log stats*/
private function findByFuelXVehicle ($dateStart = null, $dateEnd=null)
{
$query = $this->createQueryBuilder('v')
->select('v.plateNumber',
'SUM(f.fuelUsed) as totalFuel',
'SUM(f.fuelUsed*f.fuelPrice) as totalFuelCost')
->join('v.fuelPurchaces', 'f');
if ($dateStart != null && $dateEnd != null){
$query->where('f.dateTime > :dateStart and f.dateTime < :dateEnd')
->setParameter('dateStart', $dateStart)
->setParameter('dateEnd', $dateEnd);
}
$q =$query->groupBy('v.plateNumber')
->getQuery()->getResult();
return $q;
}
/* Find vehicle Job stats*/
private function findByJobXVehicle($dateStart = null, $dateEnd=null){
$query = $this->createQueryBuilder('v')
->select('v.plateNumber'
,'SUM(j.kmOdoEnd - j.kmOdoStart) as totalDistance',
'COUNT(j.idJob) as countJob')
->join('v.jobs', 'j');
if ($dateStart != null && $dateEnd != null){
$query->where('j.dateTime > :dateStart and j.dateTime < :dateEnd')
->setParameter('dateStart', $dateStart)
->setParameter('dateEnd', $dateEnd);
}
$query->groupBy('v.plateNumber');
$q = $query->getQuery()->getResult();
return $q;
}
/* Join stats for fuel logs and Jobs*/
public function findByIntegrated ($dateStart = null, $dateEnd=null)
{
$fuels = $this->findByFuelXVehicle($dateStart, $dateEnd);
$jobs = $this->findByJobXVehicle($dateStart, $dateEnd);
$jobAndFuel = null;
foreach ($jobs as $key => $job){
$jobAndFuel[$key]['plateNumber']=$job['plateNumber'];
$jobAndFuel[$key]['totalDistance']=$job['totalDistance'];
$jobAndFuel[$key]['countJob']=$job['countJob'];
}
return $this->mergeWithJobs($jobAndFuel, $fuels);
}
/* Merge arrays for fuel and jobs; join both array*/
private function mergeWithJobs ($jobAndFuel, $fuels){
//var_dump($jobAndFuel);
for ($i =0;$i<count($fuels);$i++){
$key = $this->searchArray($jobAndFuel, $fuels[$i]['plateNumber']);
if ($key != false) {
$jobAndFuel[$key]['totalFuel']=$fuels[$i]['totalFuel'];
$jobAndFuel[$key]['totalFuelCost']= round($fuels[$i]['totalFuelCost'], 2);
} else {
$aux = count($jobAndFuel);
$jobAndFuel[$aux+1]['plateNumber']=$fuels[$i]['plateNumber'];
$jobAndFuel[$aux+1]['totalFuel']=$fuels[$i]['totalFuel'];
$jobAndFuel[$aux+1]['totalFuelCost']=round($fuels[$i]['totalFuelCost'], 2);
}
}
return $jobAndFuel;
}
/*Serch for a value in a bidimantional array;
* consider moving this function to a general class*/
private function searchArray ($arrayOfResults, $needle)
{
foreach($arrayOfResults as $key => $arrayOfResult)
{
if ( $arrayOfResult['plateNumber'] === $needle )
return $key;
}
return false;
}
}
谢谢大家!