2016-08-18 7 views
3

私は、オブジェクトコレクションをapi rest controller(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); 'return "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; 
関連する問題