我有一个 HABTM 关联,无法将数据保存到连接表中。
这是我的控制器:
public function create($id)
{
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('name',$names);
$this->Invoice->create();
if(empty($this->data)){
$this->data= $this->Field->read($fields);
}
else{
if(($this->Invoice->saveAll($this->data)))
{
$this->Session->setFlash('The field has been updated');
$this->redirect(array('controller'=>'invoices', 'action'=>'index'));
}
}
}
这是表格:
<?php echo $this->Form->create('Invoice'); ?>
<?php foreach ($fields as $field): ?>
<?php echo debug($field);?>
<?php echo $this->Form->Input($field['Field']['name'], array('default' =>$field['Field']['default_value'])); ?>
<?php endforeach ;?>
<?php echo $this->Form->End('Submit');?>
当我调试时,$field
我得到这个:
模板
Employees Create New Pay Invoice Invoices Sent
\app\View\Invoices\create.ctp (line 21)
array(
'Field' => array(
'id' => '9',
'name' => 'amount',
'description' => 'amount of invoice',
'field_type' => 'int',
'field_range' => '10',
'active' => true,
'template_id' => '3',
'default_value' => ''
),
'Template' => array(
'id' => '3',
'name' => 'MGDKiallaConsulting',
'description' => 'The invoice template for MGD Kialla Pty Ltd Consulting Fees',
'account_id' => '3',
'active' => '1'
),
'Invoice' => array(
(int) 0 => array(
'id' => '1',
'active' => true,
'sender_id' => '2',
'receiver_id' => '3',
'template_id' => '1',
'created' => '2012-08-05 00:00:00',
'FieldsInvoice' => array(
'id' => '1',
'field_id' => '9',
'invoice_id' => '1',
'entered_value' => '1000.00'
)
),
(int) 1 => array(
'id' => '2',
'active' => true,
'sender_id' => '2',
'receiver_id' => '4',
'template_id' => '1',
'created' => '2012-08-05 00:00:00',
'FieldsInvoice' => array(
'id' => '2',
'field_id' => '9',
'invoice_id' => '2',
'entered_value' => '2000.00'
)
)
)
)
Amount
\app\View\Invoices\create.ctp (line 21)
array(
'Field' => array(
'id' => '10',
'name' => 'description',
'description' => 'description of invoice charges',
'field_type' => 'text',
'field_range' => null,
'active' => true,
'template_id' => '3',
'default_value' => ''
),
'Template' => array(
'id' => '3',
'name' => 'MGDKiallaConsulting',
'description' => 'The invoice template for MGD Kialla Pty Ltd Consulting Fees',
'account_id' => '3',
'active' => '1'
),
'Invoice' => array()
)
Description
\app\View\Invoices\create.ctp (line 21)
array(
'Field' => array(
'id' => '11',
'name' => 'totalacctowing',
'description' => 'total account amount owing',
'field_type' => 'int',
'field_range' => null,
'active' => true,
'template_id' => '3',
'default_value' => ''
),
'Template' => array(
'id' => '3',
'name' => 'MGDKiallaConsulting',
'description' => 'The invoice template for MGD Kialla Pty Ltd Consulting Fees',
'account_id' => '3',
'active' => '1'
),
'Invoice' => array()
)
Totalacctowing
\app\View\Invoices\create.ctp (line 21)
array(
'Field' => array(
'id' => '12',
'name' => 'pmtinfo',
'description' => 'the payment information',
'field_type' => 'text',
'field_range' => null,
'active' => true,
'template_id' => '3',
'default_value' => 'hi'
),
'Template' => array(
'id' => '3',
'name' => 'MGDKiallaConsulting',
'description' => 'The invoice template for MGD Kialla Pty Ltd Consulting Fees',
'account_id' => '3',
'active' => '1'
),
'Invoice' => array()
)
有一个fields
表,其中包含 -
id, name, default_value, template_id, active
有invoices
一张表,其中包含 -
id、sender_id、receiver_id、template_id、活动
有一张fields_invoices
桌子,里面有 -id, invoice_id, field_id, entered_value
从函数和视图中,我需要在表单中保存 invoice_id、field_id 和输入的值。