Skip to content

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

@fancyweb

Description

@fancyweb

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions