2013-12-23 5 views
1

저는 CakePHP에서 인보이스 프로젝트를하고 있습니다. 모델에서 관련 테이블을 설정했지만 어떻게 든보기로 전달하지 않습니다. CakePHP - 관련 값이 표시되지 않습니다.

이들은

내 모델입니다 :

class Invoice extends AppModel { 

    public $hasOne = array(
     'Customer' => array(
      'className' => 'Customer', 
      'foreignKey' => 'id' 
     ), 
     'Project' => array(
      'className' => 'Project', 
      'foreignKey' => 'id' 
     ),  
    ); 

    ... 

프로젝트 모델

송장 모델

class Project extends AppModel { 

    public $hasOne = array(
     'Customer' => array(
      'className' => 'Customer', 
      'foreignKey' => 'id' 
     ) 
    ); 

    public $hasMany = array( 
     'Invoice' => array(
      'className' => 'Invoice', 
      'foreignKey' => 'project_id' 
     ), 
    ); 

고객 모델

class Customer extends AppModel { 

    public $virtualFields = array(
     'full_name' => 'CONCAT(Customer.frontname, " ", Customer.lastname)', 
     'display_name' => 'IF(Customer.company > " ", Customer.company, CONCAT(Customer.frontname, " ", Customer.lastname))', 
    ); 

    public $hasMany = array(  
     'Project' => array(
      'className' => 'Project', 
      'foreignKey' => 'customer_id', 
     ), 
     'Invoice' => array(
      'className' => 'Invoice', 
      'foreignKey' => 'customer_id', 
     ), 
    ); 

그리고보기 :

<?php 
foreach($invoices as $invoice){ 
?> 

     <td><?php echo $invoice['Customer']['display_name']; ?></td>  
     <td><?php echo $invoice['Project']['project_name']; ?></td> 

여기 내 컨트롤러 : 나는 송장을 인쇄 할 경우

<?php 
class InvoicesController extends AppController { 

    public $helpers = array('Status'); 

    public function index() { 
     $uid = $this->Auth->user('cloud_id'); 
     $this->set('uid', $uid); 

     $allInvoices = $this->Invoice->find('all', array(
      'conditions' => array(
        'Invoice.cloud_id' => $this->Auth->user('cloud_id'), 
       ) 
      ) 
     ); 

     $this->set('invoices', $allInvoices); 
    } 

, 나는 관련 분야를 볼 수 있지만 값이 비어 있습니다. 이와 같이 :

Array 
(
    [Invoice] => Array 
     (
      [id] => 143 
      [cloud_id] => 1 
      [invoice_number] => 143 
      [customer_id] => 2 
      [date] => 2013-08-17 
      [period] => 30 
      [project_id] => 1 
      [status] => 1 
      [notes] => 
      [secret] => df56cd45ea7cb086458cc5c3480f77c386647a86 
     ) 

    [Customer] => Array 
     (
      [id] => 
      [cloud_id] => 
      [company] => 
      [frontname] => 
      [lastname] => 
      [address] => 

그러나 모델의 외래 키를 변경하면 열이 존재하지 않는다는 오류가 발생합니다.

여기에 무슨 문제가 있습니까?

미리 감사드립니다.

+0

컨트롤러는 어디에 있습니까? –

+1

이러한 관계가 잘못되었습니다. 모델을 ** 조건 **과 연결하고'foreignKey'를 false로 설정해보십시오. 어떤 버전의 Cake를 사용하고 있습니까? 이전 버전은 외장형 키 이름에 대해 특별히 권장하는 DB 스키마를 사용하지 않는 한 모델 링크에 끔찍합니다. –

+0

컨트롤러를 추가했습니다. 2.4.3을 사용하고 있습니다. – Vincent

답변

0

대신 HasOne 협회 시도가 BelongsTo를 사용하는,

class Invoice extends AppModel { 

    public $belongsTo = array(
     'Customer' => array(
      'className' => 'Customer', 
      'foreignKey' => 'customer_id' 
     ), 
    ); 
+0

작동! 도와 줘서 고맙다! – Vincent

+0

당신은 환영합니다 :) –

0

위의 코드에서하고있는 것처럼 테이블을 결합하지 마십시오! 이러한 관계는 정확하지 않으며 이해하기 어려울 수 있습니다.

여기 한 용액

단계 1 : 송장 모델에서 하나 개의 기능을 만들고있는 GetData 말할 수()

단계 2 : 필드 배열에서 같은 작은 수정 사항 코드 아래 그 기능 기입 내부 ...

$options = array(
      'conditions' => array('Invoice.cloud_id' => $this->Auth->user('cloud_id')), 
      'joins' => array(
       array(
        'alias' => 'Customer', 
        'table' => 'customers', 
        'type' => 'LEFT', 
        'conditions' => array(
         'Customer.id = Invoice.id', 
        ), 
       ), 
       array(
        'alias' => 'Project', 
        'table' => 'projects', 
        'type' => 'LEFT', 
        'conditions' => array(
         'Project.id = Invoice.id', 
        ), 
       ) 
      ), 
      'fields' => array('PUT DESIRED FIELDS COMMA SEPERATED') 
     ); 
    $returnData = $this->find('all',$options); 

3 단계 : 코드를 실행하면 데이터가 생성됩니다.

호프가 문제를 해결할 수 있기를 바랍니다.

관련 문제