2016-08-18 64 views
3

我想从api rest控制器(Symfony3)发送一个对象集合,但得到一个错误。 “{” 错误 “:{” 代码 “:500,” 消息 “:” 内部服务器错误”, “异常”:[{ “消息”: “通知:未定义指数:酒吧”500:内部服务器错误未定义索引Symfony3

/** 
* 
* 
* @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

你能显示完整的堆栈跟踪,允许它发现哪里错了访问情况? – xabbuh

+0

我不知道我该怎么做。这个错误只发生在我尝试发送一个对象集合时,如果我发送一个对象没有问题。对不起,我有限的英语。欢迎更正。 – DBCooper

+0

例如'$ view = $ this-> View($ name,200); return $ this-> handleView($ view); '返回“bar2”它是正确的。 – DBCooper

回答

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;