Symfony&;原则:尝试访问一对多时未定义的索引


Symfony & Doctrine: Undefined index when trying to access one-to-many

我遇到了一些问题。我有两个班:订单:

<?php
namespace AppBundle'Entity;
use Doctrine'Common'Collections'ArrayCollection;
use Doctrine'ORM'Mapping as ORM;
use AppBundle'Entity'Orderdetail;
/**
 * @ORM'Entity
 * @ORM'Table(name="carorder")
 */
class Carorder
{
    /**
     * @ORM'Column(type="integer")
     * @ORM'Id
     * @ORM'GeneratedValue
     */
    protected $id;
    /**
     * @ORM'OneToMany(targetEntity="Orderdetail", mappedBy="Carorder", cascade={"persist","remove"})
     **/
    protected $orderdetails;
    //Then all the auto genereted setters and getters beneath here

订单详情:

<?php
namespace AppBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use Doctrine'Common'Collections'ArrayCollection;
/** @ORM'Entity 
 *  @ORM'Table(name="orderdetail")
 */
class Orderdetail
{
     /**
     * @ORM'Column(type="integer")
     * @ORM'Id
     * @ORM'GeneratedValue
     */
    protected $id;
    /**
     * @ORM'ManyToOne(targetEntity="Carorder", inversedBy="orderdetails")
     **/
    protected $carorder;
     /**
     * @ORM'Column(type="integer")
     */
    protected $amount;
    //Then all the auto generated setters and getters beneath here

我无法通过Carorder访问订单详细信息。例如,这个例子,只是刺

   Undefined index: Carorder 

示例:

    $repository = $this->getDoctrine()->getRepository('AppBundle:Carorder');
    $orders = $repository->findAll();
    $orderdetail = $orders[0]->getOrderdetails()->first();

我不知道是什么原因造成的,所以我希望你们能帮我。

您映射了属性Carorder,但属性名称是carorder,区分大小写。

正确的映射可能是:

/**
 * @ORM'OneToMany(targetEntity="Orderdetail", mappedBy="carorder", cascade={"persist","remove"})
 **/
protected $orderdetails;