Nisarg Nisarg - 6 months ago 21
MySQL Question

Can not get second table value in inner join in CakePHP

I am working in CakePHP framework. I want to join two tables. So I have written query like below.

$details=$this->Deal->find('all',array('joins'=>array(
array(
'table'=>'merchants',
'alias'=>'Merchant',
'fields' => array('Merchant.username'),
'conditions' =>array('Merchant.id=Deal.merchant_id')
)
),'conditions'=>array('Deal.id'=>$id)));
echo "<pre>";
print_r($details);


When I print the details, then it will give only deal table record. Here I also want Merchant.username.

So what would I have to change in the above query?

Note :- I am working with CakePHP version 1.3.13

Answer
$details = $this->Deal->find('all', array('joins' => array(
        array(
            'table' => 'merchants',
            'alias' => 'Merchant',
            'conditions' => array('Merchant.id=Deal.merchant_id')
        )
    ), 'conditions' => array('Deal.id' => $id),
    'fields' => array('Merchant.username', 'Deal.*'),
        )
);
    echo "<pre>";
    print_r($details);