Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP: SaveAll() and hasMany data replace problem
    text
    copied!<p>I made one big form with many associations hasMany/HABTM. All is workin great on creating (!). When updating all is working nice too, but in tables where association is hasMany, data is not updated or replaced, but just inserted. This brings many rows with trash data. How can I make saveAll() do the update/replace in hasMany fields:</p> <p>Model:</p> <pre><code>class MainModel extends AppModel { var $hasAndBelongsToMany = array( 'HABTMModel1', ... 'HABTMModeln', ); var $hasMany = array( 'Model1' =&gt; array( 'dependent' =&gt; true ), ... 'Modeln' =&gt; array( 'dependent' =&gt; true ), ); } </code></pre> <p>One of the problematic hasMany models look like:</p> <pre><code>class Model1 extends AppModel { var $belongsTo = array( 'MainModel' ); } </code></pre> <p>And his table have:</p> <pre><code>id &lt;- Primary key, auto increment, int (11) main_model_id &lt;- Foreign_key int (11) name &lt;- text field, string </code></pre> <p>The $this->data is looking like:</p> <pre><code>array( [MainModel] =&gt; array( 'id' =&gt; 123 *** aditional data named identicaly to table fields (working great)*** ), [Model1] =&gt; array( [0] =&gt; array( [name] =&gt; Test1 ), [2] =&gt; array( [name] =&gt; Test2 ), ), *** all other models *** ); </code></pre> <p>Model1 table results after first creating and after updating:</p> <pre><code>id main_model_id name -------------------------------------------------------------- 11 306 Test1 12 306 Test2 13 306 Test1 (Thease are dublicates) 14 306 Test2 (Thease are dublicates) </code></pre> <p>What can I do to update/replace data in hasMany and not insert new values un edit using saveAll ? </p> <p>Thank you.</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