sql - Cakephp find 返回 null

标签 sql cakephp

大家好,我正在尝试通过发现做两件事, 型号争议属于发票。

查找所有发票

invoice.receiver_id=account.id

dispute.invoice_id=发票.id

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

这是我的争议 Controller 中的代码

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

这是在争议 Controller 中,因为我想打印有关有争议的发票的列表。

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

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

最佳答案

如果您喜欢 DisputeController,请尝试以下代码: 不知道表名是不是invoices

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

关于sql - Cakephp find 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12489238/

相关文章:

SQL - 如何将查询结果的计数添加到表中

mysql - 未知的复合列名称

php - cakePHP 中的条件

css - cakephp CSS 未加载

php - cakephp hasone 和属于加入不工作

sql - 如何使 SQL 内存优化 native 编译函数具有确定性

java - 从 Hibernate 子列表结果

php - 在 PHP 中循环并更新 sql 表的每一行

php - CakePHP 组件或其他 Controller 类?

jquery - cakephp中如何接收并处理json对象?