views:

1726

answers:

2

In my cakephp app I have an Option model.

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.

The option I alter doesn't get saved and instead a new option is inserted with the new value.

Here is my view

<h2 class='page-title' id='manage-options'>Opzioni</h2>
<?php echo $form->create(null, array('action'=>'index')); ?>
<table>
    <tr>
    <td><?= $options[0]['Option']['name']?></td>
    <td><?= $form->radio(  
                   $options[0]['Option']['id'],  
                 array(  
                   '1' => 'Sì',  
                   '0' => 'No'),  
             array('default'=> $options[0]['Option']['value'], 'legend'=>false) 
 );?>
        </td>
    </tr> 
    <tr>
        <td><?= $options[1]['Option']['name']?></td>
    <td><?= $form->input($options[1]['Option']['id'],array('label'=>false,'value' => $options[1]['Option']['value'] ))?></td>
    </tr>                                    
</table>    
<?php echo $form->submit('Salva'); ?>
<?php echo $form->end(); ?>

And my controller:

function index() {  
 if (!empty($this->data)) { 
  foreach($this->data['Option'] as $id => $value) :
   $this->Option->id = $id;       
   $feedback = $this->Option->read();  
   $this->Option->saveField('value', $value); 
  endforeach;         
  $this->Session->setFlash('Opzioni aggiornate');
 }
 $this->Option->recursive = 0;
 $this->set('options', $this->paginate());
}

Before posting here I spent two hours googling for answers and experimenting. I know about saveAll() and i have tried these solutions:

http://planetcakephp.org/aggregator/items/2172-cakephp-multi-record-forms http://teknoid.wordpress.com/2008/10/27/editing-multiple-records-with-saveall/

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.

Can you help, indicating the most proper way to do this? Cheeers, Davide

A: 

The main problem with your code that I see is that your fields, both the radio and the input, are being built with only an ID value as the first parameter. The correct "cake way" of building a field is having the first parameter be Model.fieldname, in your case I believe it would be $form->input('Option.id', array())?>

If you inspect the html generated by your code you will see the field name is data[id], and it should be data[Option][id] if you want to loop through $this->data['Option'] in your controller.

Try changing your code to include the Model.fieldname as the first parameter and then it should have the data submitted correctly to your controller.

djcode
The first parameter is not the ID, it is an array index, pointing to the Nth element of the results array. Also, the genereated input fields is perfectly correct (data[Option][id]). Thanks anyway for the suggestion but it doesn't apply.
nutsmuggler
A: 

The problem was with the data in the DB. The kind folks on the cakephp IRC channel called my attention to the fact that in most databases ID = 0 equals 'new record'. For some reason I had an option with ID 0, so when updating the underlaying mysql query actually created a new record. Changed the IDs, problem fixed.

nutsmuggler