Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use Doctrine PersistentCollection for one of my entities, another I can

I have a pair of associations. They are many-to-many and I use an explicitly created entity to join them so I can have metadata about the relationship. Though they are identical, one works and the other doesn't. Worse, last week, they both worked and I haven't touched them since. In MuSQL Workbench, I can select the correct data.

When I extract data into an array for one, life is good. When I try for the other, I get:

Call to a member function setValue() on a non-object

I also get it when I try to count() it, access it ($blah[0]) or iterate over it (foreach).

When I execute:

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;

I get:

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

Below, I include the pertinent parts of the entity definitions. I have burned hours trying this and that. I will be immensely grateful for your suggestions.

THIS ONE WORKS:

//==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();

THIS ONE DOES NOT

//==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();
like image 815
tqwhite Avatar asked Sep 01 '12 19:09

tqwhite


1 Answers

It was an error in a referencing entity purchases. It says mappedBy="purchases". It should be purchase.

Please note, the consequences of this error were impossible. It produced a huge data structure that could not be listed in almost any useful way. It gave bizarre results when touched.

The solution to this problem was that the mappedBy field name was incorrect. It did not match the actual name in the target entity (in this case, error in the Purchase entity misspelled the target association name in PurchaseOrderNodes).

It was made much more difficult to see because of the naming convention of plural table names. Watch out for that inflection!

like image 73
k0pernikus Avatar answered Nov 03 '22 14:11

k0pernikus