2013-01-31 21 views
0

執行下面的代碼提供了以下錯誤:getOneToMany返回倉庫

Warning: Invalid argument supplied for foreach()

$stock = $offerDetail->getStock(); 
foreach($stock as $s) 
{ 
    ... 
} 

返回的股票是類offerDetailRepository

這是我在offerDetail定義的關係:

/** 
* @ORM\OneToMany(targetEntity="OfferDetailStock", mappedBy="offerDetail", cascade={"remove"}) 
*/ 
protected $stock; 

and OfferDetailStock

/** 
* @ORM\ManyToOne(targetEntity="Powershop\ApplicationBundle\Entity\OfferDetail", inversedBy="stock") 
*/ 
protected $offerDetail; 

一些我在OfferDetail生成的函數:

public function getStock() 
{ 
    return $this->stock; 
} 

public function setStock($stock) 
{ 
    $this->stock = $stock; 
} 

public function addStock($stock) 
{ 
    $this->stock[] = $stock; 
} 

而且在OfferDetailStock

public function getOfferDetail() { 
    return $this->offerDetail; 
} 

public function setOfferDetail($offerDetail) { 
    $this->offerDetail = $offerDetail; 
} 

是否有人有線索?據我所知,關係的定義是正確的。 我不得不提到最初有一個ManyToMany關係類庫存,我之後提供OfferDetailStock作爲表之間。我清除了緩存並更新了方案。

感謝您的幫助提前

回答

2

輸入您的控制檯:

php app/console doctrine:generate:entities PowershopApplicationBundle:OfferDetail 

它會產生丟失的構造:

public function __construct() 
{ 
    $this->stock = new \Doctrine\Common\Collections\ArrayCollection(); 
} 
+0

謝謝!我完全忘記了構造函數。 – tmas

1

在你OfferDetail實體構造函數中添加:

public function __construct() 
{ 
    $this->stock = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

也許offerdetails沒有股票和getStocks返回null。