2

当用户在我的表单中输入数据时,它不会保存到数据库中,这是我的表的结构

Invoice - id, sender_id, receiver_id, template_id
Field - id, name, description, default_value, template_id
fields_invoices - id, invoice_id, field_id, entered_value

这是发票模型

类发票扩展 AppModel{ var $name='Invoice'; var $hasMany = array('FieldsInvoice');

这是现场模型

    var $hasMany = array(
    'FieldsInvoice'
    );

这是 fieldsInvoice 模型

<?php
class FieldsInvoice extends AppModel {
    public $belongsTo = array(
        'Field' => array(
            'className' => 'Field',
            'foreignKey' => 'field_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Invoice' => array(
            'className' => 'Invoice',
            'foreignKey' => 'invoice_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}

这是发票控制器中的创建功能

    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'))
        {

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

这是创建功能的视图

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

调试视图时,这是收到的输出

array(
    'Invoice' => array(
        'id' => ''
    ),
    'FieldsInvoice' => array(
        (int) 5 => '',
        'amount' => 'test',
        (int) 6 => '',
        'description' => 'test1',
        (int) 7 => '',
        'totalacctowing' => 'test2',
        (int) 8 => '',
        'pmtinfo' => 'test3'
    )
)
4

2 回答 2

0

代替:

$this->Invoice->save($this->request->data);

在您的 if 条件中使用以下语法;

$this->Invoice->save($this->request->data, array('deep' => true));

此链接将指导您保存相关数据。

于 2012-08-12T12:45:00.590 回答
0

你试过调试吗?尝试查看您的表单数据,看看是否所有字段都正常。使用debug()pr().

于 2012-08-12T13:22:37.397 回答