gpt4 book ai didi

php - 交响乐与学说 : Undefined index when trying to access one-to-many

转载 作者:行者123 更新时间:2023-12-05 08:33:59 25 4
gpt4 key购买 nike

我遇到了一些问题。我有 2 节课:汽车订单:

<?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 访问订单详细信息。例如这个例子,只是thorws

   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;

关于php - 交响乐与学说 : Undefined index when trying to access one-to-many,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33392533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com