如何在控制器中保存两个不同的信息,影响我的数据库中的一个表

时间:2014-05-21 16:02:22

标签: cakephp cakephp-2.1

我不知道如何在我的数据库中同时保存不同的值。我在我的观点中有这个

<?php echo $this->Form->create('SoyaPrecioInternacional');?>
    <fieldset> 

        <?php 
        echo $this->Form->input('pais', array(
            'options' => array( 
            'CHICAGO' => 'Chicago', 
            'ROSARIO' => 'Rosario'
                ),'label'=>'Ciudad'
        ));
        echo $this->Form->input('precio', array('label' => 'Ingrese en Dolares $us','style'=>'width:500px; height:30px;'));
                echo $this->Form->input('pais', array(
            'options' => array( 
            'CHICAGO' => 'Chicago', 
            'ROSARIO' => 'Rosario'
                ),'label'=>'Ciudad'
        ));
        echo $this->Form->input('precio', array('label' => 'Ingrese en Dolares $us','style'=>'width:500px; height:30px;'));
        echo $this->Form->submit('Agregar Cambio', array('class' => 'form-submit',  'title' => 'Presione aqui para agregar datos')); 
    ?>
</fieldset>

这在我的控制器中

public function add()
    {
        $this->loadModel('SoyaPrecioInternacional');
        if ($this->request->is('post')) {
            $this->request->data['SoyaPrecioInternacional']['user_id'] = $this->Auth->user('id');
            if ($this->SoyaPrecioInternacional->save($this->request->data)) {
                $this->Session->setFlash(__('La Información fue Guardada.'));
                return $this->redirect(array('action' => 'index'));
            }
        }
    }

但是当我为我的控制器中的选项1和另一个选项2定价时,只获得最后一个选择表单示例 如果我为芝加哥定价第一个选项然后罗萨里奥我的数据库中的secon选项只显示罗萨里奥值

1 个答案:

答案 0 :(得分:0)

也许

<?php echo $this->Form->create('SoyaPrecioInternacional');?>
    <fieldset> 
        <?php 
        echo $this->Form->input('SoyaPrecioInternacional.1.precio', array('label' => 'Ingrese en Dolares $us','style'=>'width:500px; height:30px;'));
        echo $this->Form->input('SoyaPrecioInternacional.1.pais', array('type' => 'hidden', 'value'=>'ROSARIO'));
            ?>

            <?php 
            echo $this->Form->input('SoyaPrecioInternacional.2.precio', array('label' => 'Ingrese en Dolares $us','style'=>'width:500px; height:30px;'));
            echo $this->Form->input('SoyaPrecioInternacional.2.pais', array('type' => 'hidden', 'value'=>'CHICAGO'));

        echo $this->Form->submit('Agregar Cambio', array('class' => 'form-submit',  'title' => 'Presione aqui para agregar datos')); 
    ?>

public function add()
{
    $this->loadModel('SoyaPrecioInternacional');

        $this->request->data['SoyaPrecioInternacional']['user_id'] = $this->Auth->user('id');
        if ($this->request->is('post')) {
            $count=0;
        foreach($this->request->data['SoyaPrecioInternacional'] as $precios){
            $count++;
            $this->SoyaPrecioInternacional->create();
            $this->request->data['SoyaPrecioInternacional']['precio']=$precios['precio'];
            $this->request->data['SoyaPrecioInternacional']['pais']=$precios['pais'];

            $this->SoyaPrecioInternacional->save($this->request->data);
        }
    return $this->redirect(array('action' => 'index'));
    }       

}