2016-08-18 57 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;