2015-10-28 54 views
1

Im有点问题。 我有2类: Carorder:Symfony&Doctrine:尝试访问一对多时未定义的索引

<?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 

的OrderDetail:

<?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访问的OrderDetail。比如这个例子,只是thorws的

Undefined index: Carorder 

例子:

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

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

+0

误差精确定位到该示例中的第一行,你提供的? –

回答

7

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

正确的映射可能是:

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

我花了2周的时间想知道这件事。我看了OneToMany映射100倍...我100%确定映射是正确的。非常感谢你 – sirius

相关问题