提问者:小点点

Cakephp查找返回空


大家好,我试着用一个发现做两件事,模型争议属于发票。

查找所有发票的位置

invoice.receiver_idaccount.id

争议。发票id=发票。id

当前,我调试时数组返回null。find语句中应该有两个结果

这是我的争端控制器的代码

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

争议有一个发送者和一个接收者,因为一张发票只能有一个争议。


共2个答案

匿名用户

如果您进入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);

匿名用户

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

 }

试试我最新的答案,让我知道