大家好我想用一个发现做两件事, 模型争议属于发票。
找到所有发票
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',
)
);
争议有一个发件人和一个收件人,因为一张发票只能有一个争议。
答案 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);
答案 1 :(得分: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);
}
尝试我的最新回答并告诉我