Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctrine 2 multi-level OneToOne Cascade
    primarykey
    data
    text
    <p>I have three Doctrine entities: Device, which has a OneToOne relationship with Device\Status, which in turn has a OneToOne relationship with Device\Status\Battery. </p> <p>I have {cascade="persist"} set between the related entities, and from what I've read, that should be all that is required for Doctrine to automatically persist each of the entities without having to do anything myself in the code.</p> <p>Here's what I'm having problems with:</p> <pre><code>$device = new \Entities\Device(); $device-&gt;setId(100); $status = $device-&gt;getStatus(); $status-&gt;setIpAddress('192.168.0.1'); $battery = $status-&gt;getBattery(); $battery-&gt;setInternalLevel(60); $em-&gt;persist($device); $em-&gt;flush(); </code></pre> <p>After executing this code, I get the following error:</p> <pre><code>Entity of type Device\Status\Battery has identity through a foreign entity Device\Status, however this entity has no identity itself. You have to call EntityManager#persist() on the related entity and make sure that an identifier was generated before trying to persist 'Device\Status\Battery'. In case of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call EntityManager#flush() between both persist operations. </code></pre> <p>My question is: what is the correct way to setup my entities to ensure that they're persisted in the correct order?</p> <p>The code for the entities can be found here: <a href="https://gist.github.com/1753524" rel="noreferrer">https://gist.github.com/1753524</a></p> <p>All tests have been performed using the Doctrine 2.2 sandbox.</p>
    singulars
    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.
 

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