2012-09-01 19 views
19

我有一對關聯。它們是多對多的,我使用一個明確創建的實體來加入它們,這樣我就可以獲得有關該關係的元數據。雖然它們是相同的,但一個是有效的,另一個是不相關的。更糟糕的是,上週他們都工作了,我從未觸及過他們。在MuSQL Workbench中,我可以選擇正確的數據。不能使用Doctrine PersistentCollection作爲我的一個實體,另一個我可以

當我將數據提取到一個數組時,生活就很好。當我嘗試其他的,我得到:

調用一個成員函數setValue()一個非對象

我也把它當我嘗試count()它,獲得它($blah[0])或迭代它(foreach)。

當我執行:

echo get_class($inData)."<BR>"; 
echo get_class($inData->accountPurchaseNodes)."<BR>"; 
echo get_class($inData->accountPurchaseNodes[0])."<BR>"; 
echo "<HR>"; 

echo get_class($inData)." x<BR>"; 
echo get_class($inData->purchaseOrderNodes)."<BR>"; 
echo get_class($inData->purchaseOrderNodes[0])."<BR>"; 
echo "<HR>"; 
exit; 

我得到:

GE\Entity\Purchase 
Doctrine\ORM\PersistentCollection 
GE\Entity\AccountPurchaseNode 

GE\Entity\Purchase 
Doctrine\ORM\PersistentCollection 

(!) Fatal error: Call to a member function setValue() on a non-object in 
/Users/tqwhite/Documents/webdev/goodEarth/goodearth.com/library/ 
Doctrine/ORM/PersistentCollection.php on line 168 

下面,我包括實體定義的相關部分。我已經燒了幾個小時嘗試這個和那個。我將非常感謝您的建議。

這一個工程:

//==Purchase Entity===================================== 

/** 
* @param \Doctrine\Common\Collections\Collection $property 
* @OneToMany(targetEntity="AccountPurchaseNode", mappedBy="account", cascade={"persist", "remove"}); 
*/ 
private $accountPurchaseNodes; 

//in __construct() 
$this->accountPurchaseNodes = new \Doctrine\Common\Collections\ArrayCollection(); 


//==AccountPurchaseNode Entity===================================== 

/** 
* 
* @ManyToOne(targetEntity="Purchase", cascade={"all"}, fetch="EAGER") 
* @JoinColumn(name="purchaseRefId", referencedColumnName="refId") 
* 
**/ 
private $purchase; 

/** 
* 
* @ManyToOne(targetEntity="Account", cascade={"all"}, fetch="EAGER") 
* @JoinColumn(name="accountRefId", referencedColumnName="refId") 
* 
**/ 
private $account; 


//==Account Entity===================================== 

/** 
* @param \Doctrine\Common\Collections\Collection $property 
* @OneToMany(targetEntity="AccountPurchaseNode", mappedBy="purchase", cascade={"persist", "remove"}); 
*/ 
private $accountPurchaseNodes; 

//in __construct() 
$this->accountPurchaseNodes = new \Doctrine\Common\Collections\ArrayCollection(); 

這一個不

//==Purchase ===================================== 

/** 
* @param \Doctrine\Common\Collections\Collection $property 
* @OneToMany(targetEntity="PurchaseOrderNode", mappedBy="purchases", cascade={"persist", "remove"}); 
*/ 
private $purchaseOrderNodes; 

//in __construct() 
$this->purchaseOrderNodes = new \Doctrine\Common\Collections\ArrayCollection(); 


//==PurchaseOrderNode ===================================== 

/** 
* 
* @ManyToOne(targetEntity="Purchase", cascade={"all"}, fetch="EAGER") 
* @JoinColumn(name="purchaseRefId", referencedColumnName="refId") 
* 
**/ 
private $purchase; 

/** 
* 
* @ManyToOne(targetEntity="Order", cascade={"all"}, fetch="EAGER") 
* @JoinColumn(name="orderRefId", referencedColumnName="refId") 
* 
**/ 
private $order; 


//==Order ===================================== 

/** 
* @param \Doctrine\Common\Collections\Collection $property 
* @OneToMany(targetEntity="PurchaseOrderNode", mappedBy="order", cascade={"persist", "remove"}); 
*/ 
private $purchaseOrderNodes; 

//in __construct() 
$this->purchaseOrderNodes = new \Doctrine\Common\Collections\ArrayCollection(); 
+6

已解決!這是一個參考實體(購買)中的錯誤。它說mappedBy =「購買」。它應該是'購買'。請注意,這個錯誤的後果是不可能的。它產生了一個龐大的數據結構,幾乎不能以任何有用的方式列出。觸摸時它給了奇怪的結果。 – tqwhite

+5

也許我應該說,爲了google和清晰度,解決此問題的方法是mappedBy字段名稱不正確。它與目標實體中的實際名稱不匹配(在此情況下,購買實體中的錯誤拼寫錯誤PurchaseOrderNodes中的目標關聯名稱)。由於複數表名的命名規則,使得看到更加困難。注意這個變化! – tqwhite

+0

你到底是怎麼發現的?我發現調試我的實體非常困難,這也可能是我的問題,但我怎麼知道? – Andre

回答

3

這是一個參照實體purchases錯誤。它說mappedBy="purchases"。它應該是purchase

請注意,這個錯誤的後果是不可能的。它產生了一個龐大的數據結構,幾乎不能以任何有用的方式列出。觸摸時它給了奇怪的結果。

此問題的解決方案是mappedBy字段名稱不正確。它與目標實體中的實際名稱不匹配(在這種情況下,Purchase實體中的錯誤在PurchaseOrderNodes中拼錯了目標關聯名稱)。

由於複數表名的命名約定,使得查看更加困難。注意這個變化!

相關問題