gpt4 book ai didi

CakePHP HABTM 数据未保存

转载 作者:行者123 更新时间:2023-12-02 17:59:19 25 4
gpt4 key购买 nike

我有一个 HABTM 关联,​​但无法将数据保存到连接表中。

这是我的 Controller :

    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
<小时/>

有一个发票表,其中包含 -

id、sender_id、receiver_id、template_id、事件

有一个 fields_invoices 表,其中包含 - id、invoice_id、field_id、entered_value

从函数和 View 中,我需要保存发票 ID、字段 ID 和表单中输入的值。

最佳答案

这是否也可以通过使用以下代码代替 $this->Invoice->saveAll(..) 来完成:

if(($this->Invoice->saveAssociated($this->data, array('deep'=> true))))

{
$this->Session->setFlash('The field has been updated');
$this->redirect(array('controller'=>'invoices', 'action'=>'index'));

}

关于saveAssociated的信息,来自公认令人困惑的文档。

关于CakePHP HABTM 数据未保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11919130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com