I have 2 entities with a OnrtoMany relationship, Vehicle that has many Jobs. I am trying to create this function at the VehicleRepository:
public function findByJobXVehicle($dateStart = null, $dateEnd=null){
$query = $this->createQueryBuilder('v')
->select('v.plateNumber','SUM(j.kmOdoEnd - j.kmOdoStart) as dist')
->join('v.jobs', 'j')
->groupBy('v.plateNumber');
$q = $query->getQuery()->getResult();
//get_class($q[0]);
return $q;
}
The object vehicle is this:
<?php
namespace TeamERP\TransportBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @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();
}
Then the Job has the following:
<?php
namespace TeamERP\TransportBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="job")
* @ORM\Entity(repositoryClass="TeamERP\TransportBundle\Entity\JobRepository")
*/
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;
}
/* My functions job cost calculator*/
public function getJobFuelCost ()
{
return $this->fuelPrice * $this->fuelUsedLitre;
}
/*My functions cost per Km */
public function getFuelCosdPerKm ()
{
if ($this->getJobFuelCost() > 0)
return round($this->getJobDistance()/$this->getJobFuelCost(),3);
return 0;
}
I am getting this error:
ContextErrorException: Notice: Undefined index: vehicles in D:\Web\wamp\www\team\vendor\doctrine\orm\lib\Doctrine\ORM\Query\SqlWalker.php line 887
I do not know where to look! Any help?
javad is correct $Vehicles mapping is pointing to non-existant variable or array collection. On your other related question with these same entities you posted the Fuel Purchase entity that may have had a curiously similar error in the inversedBy mapping.