Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp find's returning null

Tags:

sql

cakephp

hi all I'm trying to do two things with a find, the model disputes belongsTo invoices.

find all invoices where

invoice.receiver_id=account.id

and

dispute.invoice_id=invoice.id

currently the array is returning null when I debug. There should be 2 results in the find statement

here is the code in my disputes 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);
  }

This is in the disputes controller, as I want to print out a list about invoices that have disputes.

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

EDIT ---------------------

invoices tables has - id, sender_id, receiver_id, amount, expiry_date, created, paid
disputes table has - id, dispute_date, comment, active

the disputes model looks like this

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

disputes has one sender and one receiver as one invoice can have only one dispute.

like image 212
user1393064 Avatar asked Sep 19 '12 05:09

user1393064


1 Answers

try this code if you are into DisputeController: I don't know if the table name is 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);
like image 156
Alessandro Minoccheri Avatar answered Sep 22 '22 02:09

Alessandro Minoccheri