Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

php - Doctrine one-to-one unidirectional

In Symfony 3 with Doctrine I'm trying to get a one-to-one unidirectional relationship with both tables sharing the same primary key working. To do so I'm trying to replicate the example on the Doctrine Association Mapping page.

However, the one-to-one uni documentation has no examples of setters and getters - and there is no definition of the id field on the target entity, either. So I tried to experiment around myself.

These are my entities:

class Country
{

    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORMOneToOne(targetEntity="MySubEntity", cascade={"persist", "remove"})
     * @ORMJoinColumn(name="id", referencedColumnName="id", nullable=true)
     */
    private $mysubentity;
    [...]
    /**
     * @return MySubEntity
     */
    public function getMySubEntity()
    {
        return $this->mysubentity;
    }

    /**
     * @param MySubEntity $mysubentity
     */
    public function setMySubEntity($mysubentity)
    {
        $this->mysubentity = $mysubentity;
    }
}

class MySubEntity
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;

    [..]

    /**
     * Set id
     *
     * @param $id
     *
     * @return MySubEntity
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
}

When I persist the country entity, I receive Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails. When inspecting the data I can see that Doctrine attempted to set the id of MySubEntity to 0.

Does anybody have an idea what I need to do for the MySubEntity $id field to be auto-populated from the Country entity?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to change the JoinColumn name property to anything else but not id:

/**
 * @ORMOneToOne(targetEntity="MySubEntity", cascade={"persist", "remove"})
 * @ORMJoinColumn(name="mysubentity_id", referencedColumnName="id", nullable=true)
 */
private $mysubentity;

What does this do: JoinColumn tells doctrine in which database column the relation is saved. so if you call it mysub_id your main entity will have a column with that name in which the referencedColumn value will be persisted (id of your subEntity).

If you say the JoinColumn name is id which is already used by the primary key of your entity you have a conflict.

Edit:

I missed your point with sharing the same primary key. Is there any specific reason for this? But if you really need to do it for legacy reasons either look at

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html#use-case-2-simple-derived-identity

or the possibility to generate the primary key value of your subEntity by yourself by changing the generation strategy (NONE or custom in this case)

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#identifier-generation-strategies


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...