问题描述
注意:这篇文章是使用此解决的。 a>方法,问题出在我的一个实体中,因此,我尝试过的任何在正常条件下都能正常工作的东西都会因此而无法正常运行。我现在不知道如何标记它。
Note: this post was solved using this method, the problem was in one of my entities, so any thing I tried that would in normal conditions work, was bad functioning because of that. I do not know how to mark it now.
我有以下存储库实体:Vehicle,Job,FuelPurchase。
I have the following repository entities: Vehicle, Job, FuelPurchase.
对于实例变量:
/**
* @ORM\Entity
* @ORM\Table(name="vehicle")
* @ORM\Entity(repositoryClass="TeamERP\TransportBundle\Entity\VehicleRepository")
*/
class Vehicle
{
/**
* @ORM\Column(type="integer", name="id_vehicle")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $idVehicle;
/**
* @ORM\Column(type="string", length=7, name="plate_number")
*/
protected $plateNumber;
/**
* @ORM\Column(type="integer", name="distance_to_service")
*/
protected $DistanceToServiceKm;
/**
* @ORM\Column(type="integer", name="last_service_odo")
*/
protected $lastServiceODOKm;
/**
* @ORM\Column(type="string", length=100, name="make")
*/
protected $makeName;
/**
* @ORM\Column(type="string", length=100, name="model")
*/
protected $modelName;
/**
* @ORM\OneToMany(targetEntity="Job", mappedBy="vehicles")
*/
protected $jobs;
/**
* @ORM\OneToMany(targetEntity="FuelPurchase", mappedBy="vehicles")
*/
protected $fuelPurchaces;
public function __construct()
{
$this->jobs = new ArrayCollection();
$this->fuelPurchaces = new ArrayCollection();
}
...
和Fuel puchase是这样的:
and Fuel puchase is this:
/**
* @ORM\Entity
* @ORM\Table(name="fuel_purchace")
* @ORM\Entity(repositoryClass="TeamERP\TransportBundle\Entity\FuelPurchaseRepository")
*/
class FuelPurchase
{
/**
* @ORM\Column(type="integer", name="id_fuel_purchace")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $idFuelPurchace;
/**
* @ORM\Column(type="string", length=20, name="invoice_number")
*/
protected $invoice;
/**
* @ORM\Column(type="datetime", name="date_time")
*/
protected $dateTime;
/**
* @ORM\Column(type="float", name="fuel_used", nullable=true)
*/
protected $fuelUsed;
/**
* @ORM\Column(type="float", name="fuel_price", nullable=true)
*/
protected $fuelPrice;
/**
* @ORM\Column(type="float", name="km_odo", nullable=true)
*/
protected $kmOdo;
/**
* @ORM\ManyToOne(targetEntity="Vehicle", inversedBy="FuelPurchace")
* @ORM\JoinColumn(name="id_vehicle", referencedColumnName="id_vehicle")
*/
protected $vehicles;
/**
* @ORM\Column(type="string", length=250, name="refuling_remarks", nullable=true)
*/
protected $remarks;
/**
* @ORM\Column(type="string", length=100, name="refuling_person", nullable=true)
*/
protected $refueler;
...
同样的工作:
class Job
{
/**
* @ORM\Column(type="integer", name="id_job")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $idJob;
/**
* @ORM\Column(type="string", length=20, name="delivery_number")
*/
protected $deliveryNo;
/**
* @ORM\Column(type="datetime", name="date_time")
*/
protected $dateTime;
/**
* @ORM\Column(type="string", length=200, name="destination")
*/
protected $destination;
/**
* @ORM\Column(type="float", name="km_odo_start", nullable=true)
*/
protected $kmOdoStart;
/**
* @ORM\Column(type="float", name="km_odo_end", nullable=true)
*/
protected $kmOdoEnd;
/**
* @ORM\Column(type="float", name="fuel_used", nullable=true)
*/
protected $fuelUsedLitre;
/**
* @ORM\Column(type="float", name="fuel_price", nullable=true)
*/
protected $fuelPrice;
/**
* @ORM\Column(type="string", length=100, name="driver_name", nullable=true)
*/
protected $driverName;
/**
* @ORM\Column(type="string", length=250, name="crew_names", nullable=true)
*/
protected $crewNames;
/**
* @ORM\Column(type="string", length=7, name="triler_plate_number", nullable=true)
*/
protected $trilerPlateNumber;
/**
* @ORM\Column(type="string", length=500, name="remarks", nullable=true)
*/
protected $remarks;
/**
* @ORM\Column(type="string", length=500, name="return_load_plan", nullable=true)
*/
protected $returnLoadPlan;
/**
* @ORM\ManyToOne(targetEntity="Vehicle", inversedBy="job")
* @ORM\JoinColumn(name="id_vehicle", referencedColumnName="id_vehicle")
*/
protected $Vehicles;
/**
* @ORM\ManyToOne(targetEntity="JobStatus", inversedBy="job")
* @ORM\JoinColumn(name="id_job_status", referencedColumnName="id_job_status")
*/
protected $jobStatus;
/**
* @ORM\ManyToOne(targetEntity="JobType", inversedBy="job")
* @ORM\JoinColumn(name="id_job_type", referencedColumnName="id_job_type")
*/
protected $jobType;
/* My functions Distance calculator*/
public function getJobDistance ()
{
return $this->kmOdoEnd - $this->kmOdoStart;
}
....
我正在尝试在他们中的3人作了报告,但我已经开始简单了; 车辆
和 FuelPurchase
之间的联接,但我无法使其工作。检出我的Vehicle资料库:
I am trying to make joins between the 3 of them to make a report, but I have started simple; a join between Vehicle
and FuelPurchase
, but I cant make it work. Check out my Vehicle repository:
class VehicleRepository extends EntityRepository
{
public function findByCriteria ($dateStart, $dateEnd)
{
$query = $this->createQueryBuilder();
$query->select('v', 'f')
->from('TeamERPTransportBundle:Vehicle', 'v')
->innerJoin('TeamERPTransportBundle:FuelPurchase', 'f')
->groupBy('v.plateNumber')
->getQuery();
return $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
}
}
我收到此错误:
ContextErrorException:警告:缺少Doctrine\ORM\EntityRepository :: createQueryBuilder()的参数1,在D:\Web\wamp\www中调用第17行中的 teamssrcTeamERPTeamERP TransportBundle\Entity\VehicleRepository.php,并在D:\Web\wamp\www\team\vendor\doctrine\orm\中定义\lib\Doctrine\ORM\EntityRepository.php第81行
版本1:
我也尝试过以下方法:
Edition 1:I have also tryed this:
$query = $this->createQueryBuilder('v');
$query->select('v', 'f')
->from('TeamERPTransportBundle:Vehicle', 'v')
->innerJoin('TeamERPTransportBundle:FuelPurchase', 'f')
->groupBy('v.plateNumber');
$aux = $query->getQuery();
$result = $aux->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
return $result;
我收到的错误是:
[语法错误]行0,列106:错误:预期的Doctrine\ORM\Query\Lexer :: T_WITH,得到了','
从移动
:
Ramoving from
:
$query = $this->createQueryBuilder('v');
$query->select('v', 'f')
->innerJoin('TeamERPTransportBundle:FuelPurchase', 'f')
->groupBy('v.plateNumber');
$aux = $query->getQuery();
$result = $aux->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
return $result;
它会出现此错误:
[语法错误]第0行,第113行:错误:预期字面量为'BY'
[Syntax Error] line 0, col 113: Error: Expected Literal, got 'BY'
版本2:
我设法使2个表联接使用以下代码:
Edition 2:I managed to make a join with 2 tables work using the following code:
class VehicleRepository extends EntityRepository
{
public function findByCriteria ($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')
//->join('v.jobs', 'j')
->groupBy('v.plateNumber')
->getQuery()->getResult();
return $query;
}
}
现在我不能做的就是添加第三个表,如果我从作业的联接中删除注释标记,那么会收到以下错误:
ContextErrorException:注意:未定义的索引:D:\Web\wamp\www中的车辆\team\vendor\doctrine\orm\lib\Doctrine\ORM\Query\SqlWalker.php行887
Now what I cant do is to add a third table, if I remove the comment mark from the join for jobs, then I receive the following error:ContextErrorException: Notice: Undefined index: vehicles in D:\Web\wamp\www\team\vendor\doctrine\orm\lib\Doctrine\ORM\Query\SqlWalker.php line 887
有什么想法吗?
谢谢您的帮助。
Any Idea?Thank you any help will be appreciated.
推荐答案
FuelPurchace实体中的$ vehicle应该被$ fuelPurchaces逆转而不是FuelPurchace?
should $vehicles in FuelPurchace Entity be inversed by $fuelPurchaces and not FuelPurchace?
这篇关于Symfony 2加入不起作用的学说和MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!