2012-08-12 4 views
3

HABTM 연관이 있으며 조인 테이블에 데이터를 저장할 수 없습니다.CakePHP HABTM 데이터가 저장되지 않습니다.

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

mplates : 나는 $field을 디버깅 할 때 나는이 얻을

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

: 여기

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

     } 
     } 
} 

양식입니다 : 여기

내 컨트롤러 포함하는 fields 테이블 - 포함 invoices 테이블

id, name, default_value, template_id, active 

있다 - 활성

ID, SENDER_ID, receiver_id, template_id는

contians fields_invoices 테이블이 - id, invoice_id, field_id, entered_value

함수 및 뷰에서 invoice_id, field_을 (를) 저장해야합니다. id 및 입력 된 값을 양식에 입력하십시오.

+1

HABTM 조인 테이블 ('entered_value')에서 추가 데이터를 가질 수 없습니다. 레지스트리가 업데이트 될 때 지워질 것이기 때문입니다. link : (book.cakephp.org/2.0/en/models/associations-linking-models-together.html) 또한, 여러분의 코드가 무엇을하려하는지 알 수 없기 때문에 우리 중 누군가가 뭔가를 얻지 못했습니다. – petervaz

+1

join 테이블을 사용하여 hasMany를 변경하고 문제를 해결했습니다. habtm bahhh을하려고 할 때 얼마나 많은 시간을 낭비했는지 생각해보기 – user1393064

+0

이 질문에 답이 있습니까? 그렇다면 응답하거나 닫아야합니다. – Dave

답변

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에서 틀림없이 혼란 문서에서.

관련 문제