将数组保存到数据库cakephp

时间:2012-08-13 06:20:21

标签: arrays database cakephp

我有一个表单对同一个表有多个条目,我遇到的问题是它只将最后一个条目发送到数据库而不发送其余的条目。我使用了调试,它只打印出表单中的最后一个字段。

每次field次迭代都需要输入fields_invoices表格invoice_id, field_id, entered_value

这是我的表格

<?php echo $this->Form->create('FieldsInvoice'); ?>
    <?php foreach ($fields as $field): ?>
    <?php echo $this->Form->hidden('FieldsInvoice.id'); ?>
    <?php echo $this->Form->hidden('FieldsInvoice.field_id', array('default' =>$field['Field']['id'])); ?>
    <?php echo $this->Form->input('FieldsInvoice.invoice_id', array('default' =>$invoice_id, 'type'=>'hidden')); ?>
    <?php echo $this->Form->Input('FieldsInvoice.entered_value',  array('label'=>$field['Field']['name'], 'default' =>$field['Field']['default_value'])); ?>
    <?php endforeach ;?>
    <?php echo $this->Form->End('Submit');?>

这是相关的控制器

    public function create($id)
    {   
    $this->set('title_for_layout', 'Create Invoice');
    $this->set('stylesheet_used', 'homestyle');
    $this->set('image_used', 'eBOXLogoHome.png');   
    $this->layout='home_layout';


     if (!is_numeric($id)) throw new BadMethodCallException('I need an ID');
     $this->Invoice->id = $id;
     if (!$this->Invoice->exists()) throw new NotFoundException('Invalid ID');

    $this->set('invoice_id',$id);

    $names = $this->Invoice->find('list',array(
    'fields'=>array('template_id'),
    'conditions'=>array('id'=>$id)));

    $fields = $this->Field->find('all', array(
      'conditions'=>array(
     'template_id'=>$names)));

    $this->set(compact('fields'));
    $this->set(compact('invoice_id'));

    $this->set('name',$names);
    $this->Invoice->create();
    if(empty($this->data)){
        $this->data= $this->Field->read($id);
    } 
    else{
        if($this->request->is('post'))
        {
            //die(debug($this->data));

            $this->Invoice->create();
            if($this->FieldsInvoice->save($this->request->data, array('deep'=>true)));
            {
            $this->Session->setFlash('The field has been updated');
            $this->redirect(array('controller'=>'invoices', 'action'=>'index'));

            }
            //else{
            $this->Session->setFlash('Could not be saved');
            //}
        }
    }
}









\app\Controller\InvoicesController.php (line 134)
array(
    'FieldsInvoice' => array(
        (int) 0 => array(
            'id' => '',
            'field_id' => '0',
            'invoice_id' => '97',
            'entered_value' => '1'
        ),
        (int) 1 => array(
            'id' => '',
            'field_id' => '99',
            'invoice_id' => '97',
            'entered_value' => '2'
        ),
        (int) 2 => array(
            'id' => '',
            'field_id' => '999',
            'invoice_id' => '97',
            'entered_value' => '3'
        ),
        (int) 3 => array(
            'id' => '',
            'field_id' => '9999',
            'invoice_id' => '97',
            'entered_value' => '4'
        )
    )
)

1 个答案:

答案 0 :(得分:3)

要发布多个字段,请使用:

foreach ($fields as $idx => $field):
    echo $this->Form->hidden('FieldsInvoice.'.$idx.'.id');   
    echo $this->Form->hidden('FieldsInvoice.'.$idx.'.field_id', ...);
    ...

然后使用$this->FieldsInvoice->saveMany()(而不是->save())来保存多条记录。