Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Saving multiple records via Model::saveMany()</h1> <p>You don't have to manually loop over your data, CakePHP models are able to handle all this automatically, as long as you create your forms according to the CakePHP conventions</p> <h2>Creating the right Form-inputs via the FormHelper</h2> <p>For this to work correctly, your posted data should be according to the CakePHP conventions;</p> <p>For example: create your form-inputs like this:</p> <pre><code>echo $this-&gt;Form-&gt;input('Breakdown.1.field1'); echo $this-&gt;Form-&gt;input('Breakdown.1.field2'); // .... echo $this-&gt;Form-&gt;input('Breakdown.99.field1'); echo $this-&gt;Form-&gt;input('Breakdown.99.field2'); </code></pre> <p>Which, when debugging the posted data inside the controller, should look like this:</p> <pre><code>debug($this-&gt;request-&gt;data); array( 'Breakdown' =&gt; array( (int) 1 =&gt; array( 'field1' =&gt; 'value of field1 in row 1', 'field2' =&gt; 'value of field2 in row 1' ), (int) 2 =&gt; array( 'field1' =&gt; 'value of field1 in row 2', 'field2' =&gt; 'value of field2 in row 2' ) ) ) </code></pre> <h2>Saving the data - Controller code</h2> <p>Then, inside your controller:</p> <pre><code>public function qty_breakdown() { $this-&gt;layout = 'common'; if ($this-&gt;request-&gt;is('post') &amp;&amp; !empty($this-&gt;data['Breakdown'])) { if ($this-&gt;Breakdown-&gt;saveMany($this-&gt;data['Breakdown'])) { $this-&gt;Session-&gt;setFlash( 'Quantity Breakdown has been added Successfully.', 'default', array('class' =&gt; 'oMsg1 oMsgError1') ); $this-&gt;redirect('qty_breakdown'); } else { // NOTE: If you're using the FormHelper, you DON'T // have to do this, the FormHelper will automatically // mark invalid fields 'invalid' $this-&gt;set('errors', $this-&gt;Breakdown-&gt;invalidFields()); // However, it's Good practice to set a general error message $this-&gt;Session-&gt;setFlash('Unable to save your data'); } } } </code></pre> <h2>The documentation</h2> <p>This part of the documentation describes <a href="http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array" rel="nofollow">Model::saveMany()</a></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.
    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