2012-09-19 52 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

相关问题