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、および入力した値をフォームに保存する必要があります。