如何使用symfony2中的实体从数据库中获取记录?

时间:2015-06-16 09:24:09

标签: symfony

我有三张桌子相互加入 - 报价,报价车和车辆。我想记录所有车辆及其报价。那些报价不是的车辆应该返回空的报价。到目前为止,它仅记录了Vehicle表,如果我尝试获取quotevehicle表或引用的记录,则会给出错误:尝试在类“Doctrine \ ORM \ PersistentCollection”上调用方法“getQuote”。

quotevehicle的结构 id quote_id vehicle_id

Controoler

Mayavi

车辆实体

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

QuoteVehicle实体

    /**
 * @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;

引用实体

    /**
 * @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;

1 个答案:

答案 0 :(得分:0)

基于错误消息'尝试调用方法" getQuote"在课堂上#34; Doctrine \ ORM \ PersistentCollection"'听起来好像你试图在查询返回的实体上执行类似的操作:

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

Vehicle实体的$ quoteVehicle是一对多,因此是QuoteVehicles的集合。命名为$ quoteVehicles会更准确,使代码更清晰,并有助于避免错误。

您需要以下内容:

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