2012-09-19 45 views
7

大家好我試圖做兩件事情發現, 模型糾紛屬於發票。Cakephp發現返回null

找到在哪裏

invoice.receiver_id=account.id

dispute.invoice_id=invoice.id

目前,當我調試陣列返回null所有發票。應該有2個結果中查找聲明

這裏是我的糾紛控制器

public function index_admin(){ 


    $id = $this->Auth->User('account_id'); 

    $receiver = $this->Invoice->find('all',array(
    'conditions'=>array("AND"=>array(
    'Invoice.id'=>'Dispute.invoice_id', 
    'receiver_id'=>$id)))); 

     $this->set('id',$id); 
     $this->set('receiver', $receiver); 
    } 

這是在糾紛控制器,因爲我想打印出有關具有爭議的發票列表的代碼。

Query Error Affected Num. rows Took (ms) 
1 SELECT `Invoice`.`id`, `Invoice`.`scheduled`, `Invoice`.`paid`, `Invoice`.`sender_id`, `Invoice`.`receiver_id`, `Invoice`.`template_id`, `Invoice`.`created`, `Invoice`.`expiry_date`, `Invoice`.`total_amount`, `ReceiverAccount`.`id`, `ReceiverAccount`.`street`, `ReceiverAccount`.`city`, `ReceiverAccount`.`postcode`, `ReceiverAccount`.`state`, `ReceiverAccount`.`country`, `ReceiverAccount`.`active`, `ReceiverAccount`.`account_name`, `ReceiverAccount`.`abn`, `SenderAccount`.`id`, `SenderAccount`.`street`, `SenderAccount`.`city`, `SenderAccount`.`postcode`, `SenderAccount`.`state`, `SenderAccount`.`country`, `SenderAccount`.`active`, `SenderAccount`.`account_name`, `SenderAccount`.`abn`, `Template`.`id`, `Template`.`name`, `Template`.`description`, `Template`.`account_id`, `Template`.`active` FROM `pra`.`invoices` AS `Invoice` LEFT JOIN `pra`.`accounts` AS `ReceiverAccount` ON (`Invoice`.`receiver_id` = `ReceiverAccount`.`id`) LEFT JOIN `pra`.`accounts` AS `SenderAccount` ON (`Invoice`.`sender_id` = `SenderAccount`.`id`) LEFT JOIN `pra`.`templates` AS `Template` ON (`Invoice`.`template_id` = `Template`.`id`) WHERE `Invoice`.`id` = 'Dispute.invoice_id' AND `Invoice`.`receiver_id` = 2 

編輯---------------------

invoices tables has - id, sender_id, receiver_id, amount, expiry_date, created, paid 
disputes table has - id, dispute_date, comment, active 

糾紛模型看起來像這樣

public $belongsTo = array(
    'Invoice' => array(
      'className' => 'Invoice', 
      'foreignKey' => 'invoice_id', 
      'conditions' => '', 
      'fields' => '', 
      'order' => '' 
      ) 
     ); 

public $hasOne = array(
     'Sender' => array(
      'className' => 'Account', 
      'foreignKey' =>'sender_id', 
      'associationForeignKey' => 'accounts_id',), 
     'Receiver'=> array(
      'className' => 'Account', 
      'foreignKey' =>'receiver_id', 
      'associationForeignKey' => 'accounts_id', 
      ) 
      ); 

糾紛有一個發件人和一個收件人,因爲一張發票只能有一個爭議。

+0

變量$ ID返回與CORRCT價值? –

+0

是的,打印正確。 – user1393064

+0

並在查詢後返回一些錯誤?你有沒有檢查過?像沒有找到字段,沒有找到表? –

回答

1

試試這個代碼,如果你是到DisputeController: 我不知道,如果表名是發票

$id = $this->Auth->User('account_id'); 

$options['joins'] = array( 
    array(
      'table' => 'invoices', 
      'alias' => 'i', 
      'type' => 'INNER', 
      'conditions' => array(   
       'i.id' => 'invoice_id', 
        'i.receiver_id' => $id  
       )  
    )); 

$receiver = $this->Dispute->find('all', $options); 

    $this->set('id',$id); 
    $this->set('receiver', $receiver); 
+0

出現一個空數組 – user1393064

+0

在你的視圖中,如果你允許這個選項,嘗試打印這樣的查詢:<?php echo $ this-> element('sql_dump'); ?>並查看查詢是否某個字段爲空或不好 –

+0

什麼都沒有出現。 – user1393064

0
public function index_admin(){ 

    $id = $this->Auth->User('account_id'); 


    $this->Invoice->bindModel(array('hasMany'=>array('Dispute' => array ('className' => 'Dispute','foreignKey' => 'invoice_id')))); 

    $receiver = $this->Invoice->find('all',array('conditions'=>array('Invoice.receiver_id'=>$id))); 

    $this->set('id',$id); 
    $this->set('receiver', $receiver); 

    } 

============更新回答================

public function index_admin(){ 

    $id = $this->Auth->User('account_id'); 


    $this->Invoice->bindModel(array('hasMany'=>array('Dispute' => array ('className' => 'Dispute','foreignKey' => 'invoice_id','conditions'=>array('receiver_id'=>$id))))); 

    $receiver = $this->Invoice->find('all'); 

    $this->set('id',$id); 
    $this->set('receiver', $receiver); 

} 

我嘗試更新的答案,讓我知道

+0

它打印出沒有爭議的發票。 – user1393064

+0

@ user1393064:請檢查記錄中的糾紛.invoice_id = invoice.id在糾紛表 – chetanspeed511987

+0

已檢查它打印出5條記錄,只應打印3 – user1393064

相關問題