hasMany通过Join Model不保存到数据库

时间:2012-08-12 10:42:02

标签: cakephp

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

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

这是发票型号

类Invoice扩展AppModel {     var $ name =' Invoice&#39 ;;     var $ hasMany = array(         ' FieldsInvoice&#39);

这是字段模型

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

2 个答案:

答案 0 :(得分:0)

而不是:

$this->Invoice->save($this->request->data);
if条件中的

使用以下语法;

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

This link将指导您保存相关数据。

答案 1 :(得分:0)

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