gpt4 book ai didi

json - 500 : Internal Server Error undefined index Symfony3

转载 作者:行者123 更新时间:2023-12-02 03:12:37 25 4
gpt4 key购买 nike

我正在尝试从 api rest Controller (Symfony3) 发送一个对象集合,但出现错误。 "{"error":{"code":500,"message":"内部服务器错误","exception":[{"message":"注意:未定义索引:Bar"

/**
*
*
* @Get("/api/bars.{_format}", defaults={"_format"="json"}, options={"expose"=true}, name="api_bars")
* @View()
*/
public function getBarsAction (Request $request)
{
$em = $this->getDoctrine()->getManager();
$session = $this->get('Session');
$repository = $em->getRepository('AppBundle:Bar');
$bars = $repository->findAll();

foreach ($bars as $bar)
{
$id = $bar->getId();
$name = $bar->getName();
$bars[] = array('id'=>$id,'name'=>$name);

}

$view = $this->View($bars,200);
return $this->handleView($view);
}

酒吧实体是:
<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;

use Doctrine\ORM\Mapping as ORM;

/**
* Bar
*
* @ORM\Table(name="Bar")
* @ORM\Entity(repositoryClass="AppBundle\Repository\BarRepository")
*/

class Bar
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @var string
*
* @ORM\Column(name="name", type="string", length=45, nullable=true)
*/
private $name;

/**
* @var string
*
* @ORM\Column(name="location", type="string", length=45, nullable=true)
*/
private $location;


/**
* @var string
*
* @ORM\Column(name="description", type="string", length=45, nullable=true)
*/

private $description;

/**
* @ORM\OneToMany(targetEntity="Waiter", mappedBy="Bar")
*/
protected $waiters;

/**
* @ORM\OneToMany(targetEntity="Table_", mappedBy="Bar")
*/
protected $tables;

/** @ORM\OneToMany(targetEntity="Stock_food", mappedBy="Bar") */

private $stockfoods;

/** @ORM\OneToMany(targetEntity="Stock_drink", mappedBy="Bar") */

private $stockdrinks;

public function __construct()
{
$this->waiters = new ArrayCollection();
$this->tables = new ArrayCollection();
$this->stockfoods = new ArrayCollection();
$this->stockdrinks = new ArrayCollection();
}


/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* Set name
*
* @param string $name
*
* @return Bar
*/
public function setName($name)
{
$this->name = $name;

return $this;
}

/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Set location
*
* @param string $location
*
* @return Bar
*/
public function setLocation($location)
{
$this->location = $location;

return $this;
}

/**
* Get location
*
* @return string
*/
public function getLocation()
{
return $this->location;
}

/**
* Set description
*
* @param string $description
*
* @return Bar
*/
public function setDescription($description)
{
$this->description = $description;

return $this;
}

/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}


public function addWaiter($value){
$this->waiters[] = $value;
}

public function getWaiters(){
return $this->waiters;
}

public function removeWaiters($id)
{
//optionally add a check here to see that $group exists before removing it.
return $this->waiters->removeElement($id);
}

public function addTable($value){
$this->tables[] = $value;
}

public function getTables(){
return $this->tables;
}

public function removeTables($id)
{
//optionally add a check here to see that $group exists before removing it.
return $this->tables->removeElement($id);
}

public function addFood($value){
$this->stockfoods[] = $value;
}

public function getFoods(){
return $this->stockfoods;
}

public function removeFoods($id)
{
//optionally add a check here to see that $group exists before removing it.
return $this->stockfoods->removeElement($id);
}

public function addDrink($value){
$this->stockdrinks[] = $value;
}

public function getDrinks(){
return $this->stockdrinks;
}

public function removeDrinks($id)
{
//optionally add a check here to see that $group exists before removing it.
return $this->stockdrinks->removeElement($id);
}

}

非常感谢!!!!

最佳答案

我解决了我的问题!!!我与关联映射有关。我已经改变了这个:

/**
* @ORM\OneToMany(targetEntity="Waiter", mappedBy="Bar")
*/
protected $waiters;

/**
* @ORM\OneToMany(targetEntity="Table_", mappedBy="Bar")
*/
protected $tables;

/** @ORM\OneToMany(targetEntity="Stock_food", mappedBy="Bar") */

private $stockfoods;

/** @ORM\OneToMany(targetEntity="Stock_drink", mappedBy="Bar") */

private $stockdrinks;

为了这:
/**
* @ORM\OneToMany(targetEntity="Waiter", mappedBy="bar")
*/
protected $waiters;

/**
* @ORM\OneToMany(targetEntity="Table_", mappedBy="bar")
*/
protected $tables;

/** @ORM\OneToMany(targetEntity="Stock_food", mappedBy="bar") */

private $stockfoods;

/** @ORM\OneToMany(targetEntity="Stock_drink", mappedBy="bar") */

private $stockdrinks;

关于json - 500 : Internal Server Error undefined index Symfony3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39014326/

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