Search code examples
cakephpcakephp-2.1

How can I save two different information in my controller affecting one table in my DB


i don't know how to save different values in my db at the same time. i have this in my view

<?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>

and this in my controller

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'));
            }
        }
    }

but when i put a price for option 1 and another for option 2 in my controller only get the last choice form example if i put a price for chicago the first option and then rosario the secon option in my db only appears the rosario value


Solution

  • maybe

    <?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')); 
        ?>
    

    and

    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'));
        }       
    
    }