Reputation: 3609
thanks in advance to anyone who knows the answer to this as this problem makes me want to hurt kittens.
In Symfony2 I have a Doctrine entity set up like below.
namespace Product\ProductCoreBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Persistence\PersistentObject;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="item")
* @ORM\Entity(repositoryClass="Product\ProductCoreBundle\Repositories\ProductRepository")
*/
class Product extends PersistentObject
{
/**
*
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="Product\ProductCoreBundle\Entity\ItemPrice", mappedBy="product", fetch="EAGER")
*/
protected $prices;
public function __construct() {
$this->prices = new ArrayCollection();
}
}
This is very very cut down to include only the offending item. $prices should be an array of all the prices associated to this item accross all of the locales we cover (GBP, EUR etc...) and the ItemPrice entity is set up like:
namespace Product\ProductCoreBundle\Entity;
use Doctrine\Common\Persistence\PersistentObject;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="itemprice")
* @ORM\Entity(repositoryClass="Product\ProductCoreBundle\Repositories\ItemPriceRepository")
*/
class ItemPrice extends PersistentObject
{
/**
* The Product that this price is for.
* @var Product
* @ORM\ManyToOne(targetEntity="Product", inversedBy="prices")
* @ORM\JoinColumn(name="item_code", referencedColumnName="item_code")
*/
protected $product;
}
I then do a:
$pr = $this->getDoctrine()->getRepository('MPProductProductCoreBundle:Product');
$product = $pr->findByPublicId({some_id});
But I only get back the product and ONE price entry. when I look in the Symfony debug console and get the query that was executed and execute it against the database I get 2 prices back for 2 different locales, like I should do.
I'm now really stuck on fixing this one. Anyone got any clues about what I'm doing wrong?
Upvotes: 0
Views: 276
Reputation: 3609
As it turns out the fix for this was quite simple. We have a database where the item is stored separate from the item price as we support different prices in different countries. In the item price entity the item code was set as the ID of that entity. As such the entity was complaining that there was 2 rows in the database with the same "ID" and thus only returning one of them.
Upvotes: 1
Reputation: 1188
And when you delete the fetch="EAGER"
option. It's the same problem ?
Upvotes: 0