Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctrine 2.1 Identity through foreign Entities One-To-One Bidirectional Association
    primarykey
    data
    text
    <p>I'm using Doctrine 2.1 where Identity through foreign Entities is supported (Foreign Keys as Identifiers).</p> <p><a href="http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/tutorials/composite-primary-keys.html#identity-through-foreign-entities" rel="nofollow">http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/tutorials/composite-primary-keys.html#identity-through-foreign-entities</a></p> <p>Is it possible to identity through foreign Entities with a bidirectional relationship? I'm trying but I'm getting the following error when I load my data fixtures:</p> <blockquote> <p>[PDOException]</p> <p>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (<code>table</code>.<code>user</code>, CONSTRAINT <code>FK_187DDE86BF396750</code> FOREIGN KEY (<code>id</code>) REFERENCES <code>address</code> (<code>user_id</code>))</p> </blockquote> <p>Here are my entities:</p> <pre class="lang-php prettyprint-override"><code>/** * @Entity */ class User { /** @Id @Column(type="integer") @GeneratedValue */ private $id; /** @OneToOne(targetEntity="Address") */ private $address; } /** * @Entity */ class Address { /** @Id @OneToOne(targetEntity="User") */ private $user; } </code></pre> <p>If I remove the reference from User to Address the error disappears!</p> <p>I would like to be able to do the following:</p> <pre><code>$address = $user-&gt;getAddress(); </code></pre> <p>..but it seems I will have to do:</p> <pre><code>$address = $entityManager-&gt;find('Address', $user-&gt;getId()); </code></pre> <p>Is that right? Is there a way to do this?</p> <p><strong>Update 1:</strong></p> <p>I would like to be able to set the relationship from either side (bidirectional) but this does not seem possible. </p> <p>To be honest I'm not sure what the point of the inverse side of the relationship is because <strong>"Changes made only to the inverse side of an association are ignored"</strong>.</p> <p>Taken from the docs here: <a href="http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/association-mapping.html#owning-side-and-inverse-side" rel="nofollow">http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/association-mapping.html#owning-side-and-inverse-side</a> </p> <p>What is the point? </p> <p>From an Application perspective it seems to make more sense to do:</p> <p><code>$user-&gt;setAddress($address);</code> </p> <p>rather than:</p> <p><code>$address-&gt;setUser($user);</code> </p> <p>However the first isn't possible possible because:</p> <ol> <li>Making the relationship bidirectional brakes things</li> <li>The <code>User</code> side has to be the inverse side so <code>$user-&gt;setAddress($address);</code> will be ignored anyway!!</li> </ol> <p>Will someone explain this please :)</p> <p>Matthew</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload