Symfony 2加入了不工作的学说和MySQL

时间:2014-04-25 09:18:11

标签: php mysql symfony doctrine-orm dql

注意:这篇文章是使用this方法解决的,问题发生在我的一个实体中,所以我在正常情况下尝试过的任何东西都会起作用,因为那。我现在不知道如何标记它。

我有以下存储库实体:Vehicle,Job,FuelPurchase。

for intance vahicle:

/**
 * @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是这样的:

/**
 * @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个之间进行联接以进行报告,但我已经开始简单了; VehicleFuelPurchase之间的联接,但我无法使其发挥作用。查看我的车辆存储库:

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: Warning: Missing argument 1 for Doctrine\ORM\EntityRepository::createQueryBuilder(), called in D:\Web\wamp\www\team\src\TeamERP\TransportBundle\Entity\VehicleRepository.php on line 17 and defined in D:\Web\wamp\www\team\vendor\doctrine\orm\lib\Doctrine\ORM\EntityRepository.php line 81

第1版: 我也试过这个:

    $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;

我收到的错误是:

[Syntax Error] line 0, col 106: Error: Expected Doctrine\ORM\Query\Lexer::T_WITH, got ','

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'

第2版: 我设法使用以下代码使用2个表进行连接:

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: Notice: Undefined index: vehicles in D:\Web\wamp\www\team\vendor\doctrine\orm\lib\Doctrine\ORM\Query\SqlWalker.php line 887

任何想法? 谢谢你的任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

更改

$query = $this->createQueryBuilder();

$query = $this->createQueryBuilder('v');

[编辑]:

顺便说一句 - 我认为你不需要from部分:

->from('TeamERPTransportBundle:Vehicle', 'v')

因为您已经在Vehicle存储库

中创建了查询

答案 1 :(得分:0)

这是使用DQL正确编写查询的方法。

$this->getDoctrine()->getManager()
                ->createQueryBuilder()
                ->select($alias)
                ->from($table, $alias);

加入时,您可以直接使用开头的连接表的别名直接调用表名 。在这种情况下,我假设' fuel_purchase'是你的桌子。

因此,更正的查询将是,

$query = $this->getDoctrine()->getManager()
            ->createQueryBuilder()
            ->select('v', 'f')
            ->from('TeamERPTransportBundle:Vehicle', 'v')
            ->innerJoin('v.fuel_purchase', 'f')
            ->groupBy('v.plateNumber')
            ->getQuery();

return $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);

希望这会有所帮助。 干杯!

答案 2 :(得分:0)

FuelPurchace实体中的$车辆是否会被$ fuelPurchaces而不是FuelPurchace反转?