Saving id from a select form

Asked

Viewed 96 times

0

I need to save the id of a select, but in my select, is showing the name of the item, so in case I need to save in the database the id of this item.

Form:

<?=$this->Form->create('Acomodacao');?>
                        <?= $this->Form->input('acomodacoes',array(
                            'type' => 'select',
                            'options' => $acomodacoes,
                            'class' => 'form-control',
                            'empty' => false
                            )); 
                        ?>
                        <?= $this->Form->input('semanas', array(
                            'options' => $semanas,
                            'empty' => 'Até',
                            'class' => 'form-control'
                        )); ?>

Controller:

$acomodacoes = $this->Navigation->find('list',array(
    'conditions' => array(
        'parent_id' => null,
    ),
    'fields' => array(
        'Navigation.id',
        'Navigation.nome',
    ),
));
$semanas = array();          
for ($i = 1; $i < 49; $i++) {
    $semanas[$i] = "$i semana" . ($i > 1 ? "s" : "");
}

The weeks save right, but the id in my database is always equal to 0, because it is trying to save the name.

My save of the form is like this:

if ($this->request->is('post')){
        $this->Acomodacao->save($this->request->data);
}
  • First, you have two users? Second, what is your table structure acomodacoes? I imagine you have a field semanas on this table, right? And how well the weeks save right and the id No? Apparently, you just need the id to save.

  • in the select of accommodations, is returning , Flat,Hotel,Home, I need instead of saving their name, the id. My table has 3 fields. id(AI) accommodates(INT), weeks(INT). As accommodations are INT, it is saving value 0, because what the form is returning is string, I need that at the time of saving, instead of cake trying to save string in the accommodation field, it saves the id of the string that is in select.

  • Right, so the problem is with the field acomodacoes and not semanas as you let me understand in your question.

  • I said, that the weeks, are saving correctly, but in time to save the id of the facility, is saving as 0.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.