2012-09-19 61 views
1

我有一個查找參數是拋出錯誤,debugkit說未知列,現在看錶時,我可以看到它。Cakephp2.0檢索數據查找()

有一個invoices表具有 - id, sender_id, receiver_id, total_amount, expiry_date

還有具有disputes表 - id, invoice_id, active, dispute_date

這是從糾紛模型

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

這個發現是在disputesController

$id = $this->Auth->User('account_id'); 

    $conditions=array('Invoice.receiver_id' => $id, 
    'Dispute.invoice_id'=>'Invoice.id'); 

    $receiver = $this->Invoice->find('all', array(
    'conditions'=>$conditions)); 

    debug($receiver); 


    $this->set('id', $id); 
    $this->set('conditions', $conditions); 
    $this->set('receiver', $receiver); 

目前$conditions它是說,Column not found: 1054 Unknown column 'Dispute.invoice_id' in 'where clause'

如果我刪除從$conditions查找檢索調試時以下。

array(
    (int) 0 => array(
     'Invoice' => array(
      'id' => '5', 
      'scheduled' => true, 
      'paid' => false, 
      'sender_id' => '3', 
      'receiver_id' => '2', 
      'template_id' => '3', 
      'created' => '2012-02-06 00:00:00', 
      'expiry_date' => '0000-00-00', 
      'total_amount' => '0' 
     ), 
     'ReceiverAccount' => array(
      'id' => '2', 
      'street' => '50 Simmington Circuit', 
      'city' => 'Greenvale', 
      'postcode' => '3059', 
      'state' => 'VIC', 
      'country' => 'Australia', 
      'active' => true, 
      'account_name' => 'Kialla Transport (Operations) Pty Ltd', 
      'abn' => '2147483647' 
     ), 
     'SenderAccount' => array(
      'id' => '3', 
      'street' => '31 Simmington Circuit', 
      'city' => 'Greenvale', 
      'postcode' => '3059', 
      'state' => 'VIC', 
      'country' => 'Australia', 
      'active' => true, 
      'account_name' => 'MGD Kialla Pty Ltd', 
      'abn' => '2147483647' 
     ), 
     'Template' => array(
      'id' => '3', 
      'name' => 'MGDKiallaConsulting', 
      'description' => 'The invoice template for MGD Kialla Pty Ltd Consulting Fees', 
      'account_id' => '3', 
      'active' => true 
     ), 
     'FieldsInvoice' => array(), 
     'Dispute' => array(
      (int) 0 => array(
       'id' => '5', 
       'dispute_date' => '2012-03-01', 
       'comment' => 'Amount on invoice is not the same as amount discussed', 
       'active' => false, 
       'invoice_id' => '5' 
      ) 
     ) 
    ), 
    (int) 1 => array(
     'Invoice' => array(
      'id' => '16', 
      'scheduled' => false, 
      'paid' => false, 
      'sender_id' => '1', 
      'receiver_id' => '2', 
      'template_id' => '0', 
      'created' => '2012-06-20 00:00:00', 
      'expiry_date' => '0000-00-00', 
      'total_amount' => '0' 
     ), 
     'ReceiverAccount' => array(
      'id' => '2', 
      'street' => '50 Simmington Circuit', 
      'city' => 'Greenvale', 
      'postcode' => '3059', 
      'state' => 'VIC', 
      'country' => 'Australia', 
      'active' => true, 
      'account_name' => 'Kialla Transport (Operations) Pty Ltd', 
      'abn' => '2147483647' 
     ), 
     'SenderAccount' => array(
      'id' => '1', 
      'street' => '4 Ridley Court', 
      'city' => 'Doncaster East', 
      'postcode' => '3109', 
      'state' => 'VIC', 
      'country' => 'Australia', 
      'active' => true, 
      'account_name' => 'McDonalds', 
      'abn' => '2147483647' 
     ), 
     'Template' => array(
      'id' => '0', 
      'name' => 'SYSTEM', 
      'description' => 'SYSTEM', 
      'account_id' => '2', 
      'active' => true 
     ), 
     'FieldsInvoice' => array(), 
     'Dispute' => array(
      (int) 0 => array(
       'id' => '8', 
       'dispute_date' => '2012-07-01', 
       'comment' => 'Amount on invoice is not the same as amount discussed', 
       'active' => true, 
       'invoice_id' => '16' 
      ) 
     ) 
    ), 
    (int) 2 => array(
     'Invoice' => array(
      'id' => '18', 
      'scheduled' => true, 
      'paid' => false, 
      'sender_id' => '3', 
      'receiver_id' => '2', 
      'template_id' => '0', 
      'created' => '2012-06-25 00:00:00', 
      'expiry_date' => '0000-00-00', 
      'total_amount' => '0' 
     ), 
     'ReceiverAccount' => array(
      'id' => '2', 
      'street' => '50 Simmington Circuit', 
      'city' => 'Greenvale', 
      'postcode' => '3059', 
      'state' => 'VIC', 
      'country' => 'Australia', 
      'active' => true, 
      'account_name' => 'Kialla Transport (Operations) Pty Ltd', 
      'abn' => '2147483647' 
     ), 
     'SenderAccount' => array(
      'id' => '3', 
      'street' => '31 Simmington Circuit', 
      'city' => 'Greenvale', 
      'postcode' => '3059', 
      'state' => 'VIC', 
      'country' => 'Australia', 
      'active' => true, 
      'account_name' => 'MGD Kialla Pty Ltd', 
      'abn' => '2147483647' 
     ), 
     'Template' => array(
      'id' => '0', 
      'name' => 'SYSTEM', 
      'description' => 'SYSTEM', 
      'account_id' => '2', 
      'active' => true 
     ), 
     'FieldsInvoice' => array(), 
     'Dispute' => array(
      (int) 0 => array(
       'id' => '10', 
       'dispute_date' => '2012-07-01', 
       'comment' => 'Amount on invoice is not the same as amount discussed', 
       'active' => true, 
       'invoice_id' => '18' 
      ) 
     ) 
    ), 
    (int) 3 => array(
     'Invoice' => array(
      'id' => '19', 
      'scheduled' => true, 
      'paid' => false, 
      'sender_id' => '4', 
      'receiver_id' => '2', 
      'template_id' => '0', 
      'created' => '2012-07-26 00:00:00', 
      'expiry_date' => '0000-00-00', 
      'total_amount' => '0' 
     ), 
     'ReceiverAccount' => array(
      'id' => '2', 
      'street' => '50 Simmington Circuit', 
      'city' => 'Greenvale', 
      'postcode' => '3059', 
      'state' => 'VIC', 
      'country' => 'Australia', 
      'active' => true, 
      'account_name' => 'Kialla Transport (Operations) Pty Ltd', 
      'abn' => '2147483647' 
     ), 
     'SenderAccount' => array(
      'id' => '4', 
      'street' => '643 Somerton Road', 
      'city' => 'Greenvale', 
      'postcode' => '3059', 
      'state' => 'VIC', 
      'country' => 'Australia', 
      'active' => true, 
      'account_name' => 'Simmington Investments Pty Ltd', 
      'abn' => '2147483647' 
     ), 
     'Template' => array(
      'id' => '0', 
      'name' => 'SYSTEM', 
      'description' => 'SYSTEM', 
      'account_id' => '2', 
      'active' => true 
     ), 
     'FieldsInvoice' => array(), 
     'Dispute' => array() 
    ), 
    (int) 4 => array(
     'Invoice' => array(
      'id' => '20', 
      'scheduled' => false, 
      'paid' => false, 
      'sender_id' => '5', 
      'receiver_id' => '2', 
      'template_id' => '0', 
      'created' => '2012-07-28 00:00:00', 
      'expiry_date' => '0000-00-00', 
      'total_amount' => '0' 
     ), 
     'ReceiverAccount' => array(
      'id' => '2', 
      'street' => '50 Simmington Circuit', 
      'city' => 'Greenvale', 
      'postcode' => '3059', 
      'state' => 'VIC', 
      'country' => 'Australia', 
      'active' => true, 
      'account_name' => 'Kialla Transport (Operations) Pty Ltd', 
      'abn' => '2147483647' 
     ), 
     'SenderAccount' => array(
      'id' => '5', 
      'street' => '2 Everingham Court', 
      'city' => 'Greenvale', 
      'postcode' => '3059', 
      'state' => 'VIC', 
      'country' => 'Australia', 
      'active' => true, 
      'account_name' => 'NG Technologies', 
      'abn' => '2147483647' 
     ), 
     'Template' => array(
      'id' => '0', 
      'name' => 'SYSTEM', 
      'description' => 'SYSTEM', 
      'account_id' => '2', 
      'active' => true 
     ), 
     'FieldsInvoice' => array(), 
     'Dispute' => array() 
    ), 
    (int) 5 => array(
     'Invoice' => array(
      'id' => '21', 
      'scheduled' => false, 
      'paid' => false, 
      'sender_id' => '6', 
      'receiver_id' => '2', 
      'template_id' => '0', 
      'created' => '2012-07-30 00:00:00', 
      'expiry_date' => '2010-11-10', 
      'total_amount' => '0' 
     ), 
     'ReceiverAccount' => array(
      'id' => '2', 
      'street' => '50 Simmington Circuit', 
      'city' => 'Greenvale', 
      'postcode' => '3059', 
      'state' => 'VIC', 
      'country' => 'Australia', 
      'active' => true, 
      'account_name' => 'Kialla Transport (Operations) Pty Ltd', 
      'abn' => '2147483647' 
     ), 
     'SenderAccount' => array(
      'id' => '6', 
      'street' => '20 Ridley Court', 
      'city' => 'Doncaster', 
      'postcode' => '3109', 
      'state' => 'VIC', 
      'country' => 'Australia', 
      'active' => true, 
      'account_name' => 'KFC', 
      'abn' => '2147483647' 
     ), 
     'Template' => array(
      'id' => '0', 
      'name' => 'SYSTEM', 
      'description' => 'SYSTEM', 
      'account_id' => '2', 
      'active' => true 
     ), 
     'FieldsInvoice' => array(), 
     'Dispute' => array() 
    ) 
) 

我想要做的是隻有他們有即dispute.invoice_id = invoice.id糾紛信息,但它拋出一個錯誤檢索發票信息。有任何想法嗎?

回答

0

您可以使用臨時刪除協會這是沒有用在你的方法來檢索數據

$ this-> User-> unbindModel(array('belongsTo'=> array('Group'),'hasOne'=> array('UsersChicken')));

類型函數

0

嘗試使用中可容納行爲:

$this->Invoice->Behaviors->attach('Containable'); 

$this->Invoice->contain('Dispute' => array('conditions' => array('Dispute.invoice_id' => 'Invoice.id'))); 
$receiver = $this->Invoice->find('all', array('conditions' => array('Invoice.receiver_id' => $id))); 

$this->Invoice->Behaviors->detach('Containable'); 

參考:

+0

它不喜歡你放在那裏的第二行代碼。 – user1393064