Note that there are some explanatory texts on larger screens.

plurals
  1. POCakephp's beforeSave not retaining new data when using saveAll?
    primarykey
    data
    text
    <p>I have a relatively simple entries model with just five fields:</p> <ol> <li>id</li> <li>type (what datatype this entry is)</li> <li>amount (how many of whatever type it is)</li> <li>unit (the unit of the type)</li> <li>date (the datettime when this entry was entered)</li> <li>user_id (the id of the user who enters</li> </ol> <p>So, nothing fancy. Now a single form can have multiple entries (both already existing ones and new ones just created), the form is extended via ajax calls. </p> <p>When I submit the form <code>$this-&gt;data</code> looks like this:</p> <pre><code>Array ( [Entry] =&gt; Array ( [date] =&gt; 2011-01-07 [0] =&gt; Array ( [id] =&gt; 1 [type] =&gt; Eat [amount] =&gt; 1 Steak, one baked potatoe [unit] =&gt; lunch [time] =&gt; Array ( [hour] =&gt; 13 [min] =&gt; 31 ) ) [1] =&gt; Array ( [type] =&gt; weight [amount] =&gt; 78.5 [unit] =&gt; KG [time] =&gt; Array ( [hour] =&gt; 22 [min] =&gt; 22 ) ) ) ) </code></pre> <p>The first entry in <code>$this-&gt;data['Entry']['date']</code> is the date that shall be used by ALL the entries. And since also the user_id is missing I created a "beforeSave" function in the entry-model. It looks like this:</p> <pre><code>function beforeSave() { App::import('Component','Session'); $this-&gt;Session = new SessionComponent(); if (isset($this-&gt;data) &amp;&amp; isset($this-&gt;data['Entry'])) { $date = $this-&gt;data['Entry']['date']; unset($this-&gt;data['Entry']['date']); foreach ($this-&gt;data['Entry'] as $n =&gt; $entry) { if (is_array($entry)) { $this-&gt;data['Entry'][$n]['date'] = $date . ' ' . $entry['time']['hour'] . ':' . $entry['time']['min'] . ':00'; $this-&gt;data['Entry'][$n]['user_id'] = $this-&gt;Session-&gt;read('Auth.User.id'); } } debug($this-&gt;data); } return true; } </code></pre> <p>I remove the date, add it together with the time entry of the user, thus creating a mysql datetime entry and add the user_id of the logged in user. Straightforward, really. The resulting array (as output by that last <code>debug()</code>) looks like the following:</p> <pre><code>Array ( [Entry] =&gt; Array ( [0] =&gt; Array ( [id] =&gt; 1 [type] =&gt; Eat [amount] =&gt; 1 Steak, 1 baked potatoe [unit] =&gt; lunch [time] =&gt; Array ( [hour] =&gt; 09 [min] =&gt; 31 ) [date] =&gt; 2011-01-07 09:31:00 [user_id] =&gt; 2 ) [1] =&gt; Array ( [type] =&gt; Weight [amount] =&gt; 78.5 [unit] =&gt; KG [time] =&gt; Array ( [hour] =&gt; 22 [min] =&gt; 22 ) [date] =&gt; 2011-01-07 22:22:00 [user_id] =&gt; 2 ) ) ) </code></pre> <p>So it look exactly like I want it to look and it should be easily saved. But when I use <code>$this-&gt;Entry-&gt;saveAll($this-&gt;data['Entry']</code>) to save all the entries, not only does it not work, but when I debug <code>$this-&gt;data</code> directly after the saveAll, it looks exactly like before the saveAll function - the date is back in the array, the entries do not have a date or user_id entry. </p> <p>I can see that beforeSave is called, I can see that it changes <code>$this-&gt;data</code>, but somewhere between the end of beforeSave and the usage of "saveAll" all my changes get lost and <code>$this-&gt;data</code> is reverted to it's original state. Therefore no saving takes place.</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.
 

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