[ORM] Entity with composite primary key requires __toString() to return the entity id
Opened this issue · 1 comments
I have an entity that has a double primary / foreign key as identifier (like in a ManyToMany
association) :
class Foo
{
/**
* @var Bar
*
* @ORM\ManyToOne(targetEntity="Bar")
* @ORM\Id
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $bar;
/**
* @var Qux
*
* @ORM\ManyToOne(targetEntity="Qux")
* @ORM\Id
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $qux;
public function getBar()
{
return $this->bar;
}
public function getQux()
{
return $this->qux;
}
....
I want to use it as a component :
...
$actionManager->findOrCreateComponent($foo)
The DoctrineComponentDataResolver
class casts as string the value returned by the bar
and qux
properties getters to determine the identifier stored in the Component
entity (https://github.com/stephpy/timeline-bundle/blob/master/ResolveComponent/DoctrineComponentDataResolver.php#L54). This identifier is also used to check if the component already exists or no.
So I have to implement the __toString()
method in the Bar
and Qux
classes and they must absolutely return an identifier (most likely the entity id), because I need it later in my custom Locator
for example. That prevents me from using the __toString()
method for a real representation as a string of the concerned entities.
IMO, in the DoctrineComponentDataResolver
, if the getter of the field returns an object, it should try to load its metadata. If the object is an entity, it should return its identifier.
Hi,
I understand your issue.
Unfortunately, I guess we don't support components where primary key is a relation ...
If you find something to make it work, 👍