Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your problem is not related to "Foreign Keys as Identifiers", but to owning-side of a relation :</p> <p>In Doctrine, a relation may be bidirectional, but in this case the relation must have an "onwing-side". That means :</p> <ul> <li><p>on your "entities part" : both your entities have an attribute referencing each other. In your case you have $address in User and $user in Address, and you can use $user->getAddress() and $address->getUser()</p></li> <li><p>on your "sql part" : only one table, the owning-side, have an attribute referencing the other table.</p></li> </ul> <p>You can inform Doctrin what is the owning side by using "mappedBy" on the non-owning side. In your situation, with Foreign Key as Identifiers, you have not the choice : the owning-side is the one with Foreign Key. So, test this :</p> <pre><code>/** * @Entity */ class User { /** @Id @Column(type="integer") @GeneratedValue */ private $id; /** @OneToOne(targetEntity="Address", mappedBy="user") */ private $address; } /** * @Entity */ class Address { /** @Id @OneToOne(targetEntity="User", inversedBy="address")) */ private $user; } </code></pre> <p>Another important point : In a bidirectional relation, Doctrine can't automaticaly update the other side when you set the non-owning side. So you have to do it manualy. For not having do think about this each time you create new users and addresses, you can automate the process in your setter :</p> <pre><code>// in class User : public function setAddress($address) { $address-&gt;setUser($this); // &lt;= here it is !!! $this-&gt;address = $address; return $this; } // in class Address : public function setUser($user) { $this-&gt;user = $user; return $this; } </code></pre> <p>Now, you can set address like you want, without bothering about this owning-side-things :</p> <pre><code>$address = new Addresse(); $address-&gt;set... $user-&gt;setAddress($address); </code></pre>
 

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