Monday, July 25, 2011

Checkbox múltiples + HABTM

Hello. I can't save multiple checkbox in HABTM.

My code is:

MODEL

<?php

class Turn extends AppModel {
    var $name = 'Turn';
    
    var $hasAndBelongsToMany = array( 'Pharmacy' => array( 'className' => 'Pharmacy' ) );
    
    var $validate = array(
'name' => array(
'rule' => 'notEmpty'
),
);
}

?>

<?php

class Pharmacy extends AppModel {
    var $name = 'Pharmacy';
    
    var $hasAndBelongsToMany = array( 'Turn' => array( 'className' => 'Turn',
                                                'joinTable' => 'pharmacies_turns'));
    
    var $validate = array(
'name' => array(
'rule' => 'notEmpty'
),
'address' => array(
'rule' => 'notEmpty'
),
'phonenumber' => array(
'rule' => 'notEmpty'
)
);
}

?>

CONTROLLER:

function add() {
$this->layout = 'defaultBackend';
if (!empty($this->data)) {
$this->Turn->Pharmacy->create();
if ($this->Turn->save($this->data)) {
$this->Session->setFlash('El turno ha sido grabado.');
$this->redirect(array('action'=>'index'));
}
}else{
$pharmacy = new Pharmacy();
$this->set('pharmacies', $pharmacy->find('all'));    
}
}

VISTA:

<?php
echo $form->create('Turn');
echo $form->input('name', array('size' => '45',  "label" => "Nombre"));
echo $form->input('sequence', array('size' => '2',  "label" => "Nro Secuencia"));
?>
<?php
$i = 0; 
foreach ($pharmacies as $pharmacy): 
$i = $i + 1;
?>
<tr>
<td><?php echo $form->checkbox('Pharmacy.id', array('value' => $pharmacy['Pharmacy']['id'], 'hiddenField' => false)); ?></td>
<td><?php echo $pharmacy['Pharmacy']['name']; ?></td>
        <td><?php echo $pharmacy['Pharmacy']['address']; ?></td>                
</tr>
<?php endforeach; ?>

<?php
echo $form->end('Guardar');
?>

Can you help me?
Thanks!!!

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: