Doctrine 2. How to force entity from proxy
I have 3 entities:
/**
* @ORM\Entity
* @ORM\Table(name="table_a")
*/
class A
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
* ORM\OneToMany(targetEntity="B", mappedBy="entityA")
*/
protected $entitiesB;
/**
* ORM\OneToMany(targetEntity="C", mappedBy="entityA")
*/
protected $entitiesC;
/**
* @ORM\Column(type="string")
*/
protected $name;
}
/**
* @ORM\Entity
* @ORM\Table(name="table_b")
*/
class B
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
* ORM\ManyToOne(targetEntity="A", inversedBy="entitiesB")
*/
protected $entityA;
/**
* @ORM\Column(type="date")
*/
protected $date;
}
/**
* @ORM\Entity
* @ORM\Table(name="table_c")
*/
class C
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
* ORM\ManyToOne(targetEntity="A", inversedBy="entitiesC")
*/
protected $entityA;
/**
* @ORM\Column(type="string")
*/
protected $description;
}
And I have the following situation:
$eB = $repositoryB->find(1);
$eA = $eB->getEntityA(); // $eA will be a proxy
$eC = new C();
$eC->setDescription('XXXXXXXXXX')
->setEntityA($eA);
This will generate an error because $eA is a proxy not an entity. Even if
I try:
$eB = $repositoryB->find(1);
$eA = $repositoryA->find(1);
$eC = new C();
$eC->setDescription('XXXXXXXXXX')
->setEntityA($eA);
Will still get an error because once you have fetched a B entity it will
automatically fetch a proxy of A entity. And when you try to fetch the A
entity with the same identifier as the proxy Doctrine will return the
proxy object from the Identity Map because you can not have two objects
(one proxy and one Entity) for the same db record.
So is there a way to force retrieving an entity from its proxy? Or another
way to set an association, by id not by entity?
No comments:
Post a Comment