我试图从api rest controller(Symfony3)发送一个对象集合,但是收到错误。 " {"错误":{"代码":500,"消息":"内部服务器错误","例外":[{" 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);
}
}
非常感谢!!!!
答案 0 :(得分:1)
我解决了我的问题!我与关联映射有关。我改变了这个:
/**
* @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;