Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctrine 1.2 save Record relations UPDATE rather than INSERT
    text
    copied!<p>I am trying to save in cascade some users:</p> <pre><code> $user = new User(); $user-&gt;name = 'xxx'; $user-&gt;location-&gt;id = 1; $user-&gt;location-&gt;name = 'yyy'; $user-&gt;save; $user2 = new User(); $user2-&gt;name = 'zzz'; $user2-&gt;location-&gt;id = 1; $user2-&gt;location-&gt;name = 'yyy'; $user2-&gt;location-&gt;zip = '123456'; $user2-&gt;save; </code></pre> <p>In this situation I wish Doctrine to be smart enough and update the location 1 since I am changing the content for the id 1, but what I have instead is another insert. I tried to workaround using a preSave() method inside User:</p> <pre><code>public function preSave( Doctrine_Event $event ) { $invoker = $event-&gt;getInvoker(); if ( /...decide to UPDATE the record .../ ) { $invoker-&gt;state( Doctrine_Record::STATE_DIRTY ); } else { $invoker-&gt;state( Doctrine_Record::STATE_CLEAN ); } } </code></pre> <p>but when doctrine tries to UPDATE it doesn't have identifier and produces this error:</p> <pre><code>Doctrine_Connection_Mysql_Exception: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens </code></pre> <p>Isn't this something that Docrtrine should provide out of the box? Am I missing something here? Why do I need to implement this behavior by hand?</p> <hr> <h2>Further Notes</h2> <p>We have 3 cases that dictate whether the record should be inserted, updated, or simply related:</p> <h3>1</h3> <ul> <li>Brand new user to our database - should be inserted</li> <li>Brand new location to our database - should be inserted</li> </ul> <h3>2</h3> <ul> <li>Brand new user to our database - should be inserted</li> <li>Existing location - should be linked to user record</li> </ul> <h3>3</h3> <ul> <li>Brand new user to our database - should be inserted</li> <li>Existing location id, updated data - should be updated and linked to user record</li> </ul> <p>We need to find the most efficient way to do this. Obviously we can do a multitude of selects in preSave() etc, we just need to get the most out of Doctrine</p>
 

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