7

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

查找所有发票

invoice.receiver_id=account.id

dispute.invoice_id=invoice.id

当前,当我调试时,该数组返回 null。find 语句中应该有 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',
            )
            );

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

4

2 回答 2

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);
于 2012-09-19T06:53:32.713 回答
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);

 }

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

于 2012-09-19T06:44:30.810 回答