Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

[ORM] Entity with composite primary key requires __toString() to return the entity id #190

Open
fancyweb opened this issue Apr 28, 2015 · 1 comment

Comments

@fancyweb
Copy link

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 barand 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.

@stephpy
Copy link
Owner

stephpy commented Apr 28, 2015

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, 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants