2012-08-12 138 views
3

我有一个HABTM关联,无法将数据保存到连接表。CakePHP 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我得到这个:

mplates

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和表单中的输入值。

+1

在HABTM连接表('entered_value')上不能有额外的数据,因为它在注册表更新时将被删除。链接:(book.cakephp.org/2.0/en/models/associations-linking-models-together.html)另外,我们中的一个没有得到某些东西,因为我无法理解你的代码试图做什么。 – petervaz 2012-08-13 20:56:45

+1

我通过连接表更改为hasMany并解决了问题。想想我浪费多少时间试图做一个贱民bahhh – user1393064 2012-08-14 04:25:15

+0

这个问题的答案呢?如果是这样,应该回答或关闭。 – Dave 2012-10-22 00:39:52

回答

0

难道这不也是被代替$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,从公认的混乱文档。