Note that there are some explanatory texts on larger screens.

plurals
  1. POEdit and save multiple records in cakephp
    primarykey
    data
    text
    <p>In my cakephp app I have an Option model.</p> <p>In my option/index view I display 2 options with inputs and radio button fields. I want to update both of them, but I get a weird behaviour. </p> <p>The option I alter doesn't get saved and instead a new option is inserted with the new value.</p> <p>Here is my view</p> <pre><code>&lt;h2 class='page-title' id='manage-options'&gt;Opzioni&lt;/h2&gt; &lt;?php echo $form-&gt;create(null, array('action'=&gt;'index')); ?&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;?= $options[0]['Option']['name']?&gt;&lt;/td&gt; &lt;td&gt;&lt;?= $form-&gt;radio( $options[0]['Option']['id'], array( '1' =&gt; 'Sì', '0' =&gt; 'No'), array('default'=&gt; $options[0]['Option']['value'], 'legend'=&gt;false) );?&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;?= $options[1]['Option']['name']?&gt;&lt;/td&gt; &lt;td&gt;&lt;?= $form-&gt;input($options[1]['Option']['id'],array('label'=&gt;false,'value' =&gt; $options[1]['Option']['value'] ))?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;?php echo $form-&gt;submit('Salva'); ?&gt; &lt;?php echo $form-&gt;end(); ?&gt; </code></pre> <p>And my controller:</p> <pre><code>function index() { if (!empty($this-&gt;data)) { foreach($this-&gt;data['Option'] as $id =&gt; $value) : $this-&gt;Option-&gt;id = $id; $feedback = $this-&gt;Option-&gt;read(); $this-&gt;Option-&gt;saveField('value', $value); endforeach; $this-&gt;Session-&gt;setFlash('Opzioni aggiornate'); } $this-&gt;Option-&gt;recursive = 0; $this-&gt;set('options', $this-&gt;paginate()); } </code></pre> <p>Before posting here I spent two hours googling for answers and experimenting. I know about <code>saveAll()</code> and i have tried these solutions:</p> <p><a href="http://planetcakephp.org/aggregator/items/2172-cakephp-multi-record-forms" rel="nofollow noreferrer">http://planetcakephp.org/aggregator/items/2172-cakephp-multi-record-forms</a> <a href="http://teknoid.wordpress.com/2008/10/27/editing-multiple-records-with-saveall/" rel="nofollow noreferrer">http://teknoid.wordpress.com/2008/10/27/editing-multiple-records-with-saveall/</a></p> <p>I have been tweaking my code to fit these patterns, but I got no results (oscillating between 'not working' and 'not working and I get an extra record'), so I decided to post my original code.</p> <p>Can you help, indicating the most proper way to do this? Cheeers, Davide</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