2015-06-16 30 views
1

我已經有三個表互相連接 - 報價,報價車和車輛。我想獲得所有車輛和他們的報價記錄。那些報價不合適的車輛應該返回空報價。到目前爲止,它只給出Vehicle表的記錄,並且如果我嘗試獲取quotevehicle表的記錄或引用它給出錯誤:試圖在類「Doctrine \ ORM \ PersistentCollection」上調用方法「getQuote」。如何使用symfony2中的實體從數據庫獲取記錄?

quotevehicle ID quote_id結構vehicle_id

Controoler

 $qb = $om->createQueryBuilder() 
      ->from('MinicaspVehicleBundle:Vehicle', 'v') 
      ->select('v', 'b') 
      ->leftJoin('v.quoteVehicle', 'b') 
      ->orderBy('v.id', 'ASC') 
    ; 

車輛實體

/** 
* @ORM\Column(type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @ORM\Column(type="smallint") 
*/ 
protected $seats; 

/** 
* @ORM\Column(length=45) 
*/ 
protected $fleet; 

/** 
* @ORM\Column(length=45) 
*/ 
protected $regNo; 

/** 
* @ORM\Column(length=45, nullable=true) 
*/ 
protected $auto; 

/** 
* @ORM\Column(length=45, nullable=true) 
*/ 
protected $fuel; 

/** 
* @ORM\Column(nullable=true) 
*/ 
protected $notes; 

/** 
* @ORM\Column(length=45, nullable=true) 
*/ 
protected $make; 

/** 
* @ORM\Column(length=45, nullable=true) 
*/ 
protected $model; 

/** 
* @ORM\Column(length=45, nullable=true) 
*/ 
protected $wheelChairsNumber; 

/** 
* @ORM\Column(type="smallint", nullable=true) 
*/ 
protected $courierSeats; 

/** 
* @ORM\Column(type="smallint", nullable=true) 
*/ 
protected $wc; 

/** 
* @ORM\Column(length=45, nullable=true) 
*/ 
protected $chassis; 

/** 
* @ORM\Column(type="decimal", nullable=true) 
*/ 
protected $engineCapacity; 

/** 
* @ORM\Column(type="array", nullable=true) 
*/ 
protected $additionalCosts; 

/** 
* @ORM\Column(nullable=true) 
*/ 
protected $motLicense; 

/** 
* @ORM\Column(type="date", nullable=true) 
*/ 
protected $motLicenseStart; 

/** 
* @ORM\Column(type="date", nullable=true) 
*/ 
protected $motLicenseExpiry; 

/** 
* @ORM\Column(nullable=true) 
*/ 
protected $insurancePolicy; 

/** 
* @ORM\Column(type="date", nullable=true) 
*/ 
protected $insurancePolicyStart; 

/** 
* @ORM\Column(type="date", nullable=true) 
*/ 
protected $insurancePolicyExpiry; 

/** 
* @ORM\Column(type="date", nullable=true) 
*/ 
protected $nextServiceDate; 

/** 
* @ORM\Column(type="date", nullable=true) 
*/ 
protected $nextInspectionDate; 

/** 
* @ORM\Column(type="date", nullable=true) 
*/ 
protected $roadTaxExpiry; 

/** 
* @ORM\Column(type="boolean", unique=true, nullable=true) 
*/ 
protected $isDefault; 

/** 
* @ORM\Column(type="boolean") 
*/ 
protected $isActive = true; 

/** 
* @ORM\Column(type="array", nullable=true) 
*/ 
protected $fleetmaticsData; 

/** 
* @ORM\ManyToOne(targetEntity="VehicleCat") 
*/ 
protected $vehicleCat; 

/** 
* @ORM\OneToMany(targetEntity="VehicleFile", mappedBy="vehicle", cascade={"persist"}) 
*/ 
protected $vehicleFiles; 

/** 
* @ORM\OneToMany(targetEntity="Probus\QuoteExtraBundle\Entity\QuoteVehicle", mappedBy="vehicle", cascade={"persist"}) 
* @ORM\JoinColumn() 
*/ 
protected $quoteVehicle; 

/** 
* @ORM\ManyToMany(targetEntity="Probus\QuoteExtraBundle\Entity\Tag") 
* @ORM\JoinTable(
*  joinColumns={@ORM\JoinColumn(onDelete="CASCADE")}, 
*  inverseJoinColumns={@ORM\JoinColumn(onDelete="CASCADE")} 
*) 
*/ 
protected $tags; 

QUOT eVehicle實體

/** 
* @ORM\Column(type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @ORM\ManyToOne(targetEntity="Quote", inversedBy="vehicles") 
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE") 
*/ 
protected $quote; 

/** 
* @Gedmo\Versioned 
* @ORM\ManyToOne(targetEntity="Minicasp\VehicleBundle\Entity\Vehicle") 
* @ORM\JoinColumn() 
*/ 
protected $vehicle; 

/** 
* @Gedmo\Versioned 
* @ORM\ManyToOne(targetEntity="Minicasp\DriverBundle\Entity\Driver") 
*/ 
protected $driver; 

/** 
* @Gedmo\Versioned 
* @ORM\Column(type="boolean") 
*/ 
protected $driverConfirmed = false; 

/** 
* @Gedmo\Versioned 
* @ORM\Column(type="boolean") 
*/ 
protected $textDriver = false; 

/** 
* @Gedmo\Versioned 
* @ORM\Column(type="boolean") 
*/ 
protected $emailDriver = false; 

/** 
* @Gedmo\Versioned 
* @ORM\Column(type="boolean") 
*/ 
protected $textCommutingDriver = false; 

/** 
* @Gedmo\Versioned 
* @ORM\Column(type="boolean") 
*/ 
protected $workTicket = false; 

報價實體

/** 
* @ORM\OneToMany(targetEntity="QuoteVehicle", mappedBy="quote", orphanRemoval=true, cascade={"persist"}) 
*/ 
protected $vehicles; 
+0

我希望這是一個錯字,但你的描述和代碼參考「quotevehicle」,但你quotevehicle代碼標題是「VehicleQuote」。標題是否是錯字或者您是否錯誤地命名了實體類? – redbirdo

+0

當你說'如果我試圖獲得報價表的記錄或者報價給出錯誤',那麼錯誤是什麼? – redbirdo

+0

有什麼特別的原因,你爲什麼不使用這個?:$ vehicles = $ vehicleRepository-> findBy(array(),array('id'=>'ASC')); – redbirdo

回答

0

基於錯誤信息 '試圖調用方法 「的getQuote」 階級 「學說\ ORM \ PersistentCollection」' 聽起來好像你是試圖在您的查詢所返回的實體上執行如下操作:

$quote = $vehicle->getQuoteVehicle()->getQuote(); 

車輛實體的$ quoteVehi cle是一對多的,因此是一個QuoteVehicles集合。命名它$ quoteVehicles會更準確,使代碼更清晰並有助於避免錯誤。

你需要的東西,如:

foreach($vehicle->getQuoteVehicles() as $quoteVehicle) 
{ 
    $quote = $quoteVehicle->getQuote(); 
    ... 
} 
相關問題