2013-12-23 109 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?除非你特別使用他們推薦的數據庫模式(對於外鍵名稱),否則舊版本對於鏈接模型是很可怕的 –

+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步:執行你的代碼,它會產生數據。

希望它能解決您的問題。