php - cakephp join query is not working

153

Solution:

Kindly check the following your file:

Your controller top "$uses" array contain "DealPurchase" Example

class MerchantController extends AppController {
    var $uses = array(
        'DealPurchase'
    );

Try the following

$query['conditions'] = array('DealPurchase.deal_id'=>$deal_id);
$query['fields'] = array('DealPurchase.*','User.*');
$query['joins'] = array(
                    array(
                        'table'=>'users',
                        'alias'=>'User',
                        'type'=>'INNER',
                        'conditions'=>array('User.id = DealPurchase.user_id')
                        ),
                );
$query['group'] = array('DealPurchase.user_id');
$result = $this->DealPurchase->find('all', $query);

database table deal_purchases For Model Name should beDealPurchase.php in the following format

<?php 
class DealPurchase extends AppModel {
    var $name = "DealPurchase";
    //Relationship some tables
    //Validation here
}
?>

User.php Model(users)

class User extends AppModel {
    var $name = "User"; 
    var $hasMany = array(
        'DealPurchase' => array(
            'className' => 'DealPurchase',
            'foreignKey' => 'user_id',
            'dependent' => true //true means when delete the user entry via coding then also deleted the deal_purchases entry also
        )
    );
    //validation
}

DealPurchase.php Model(deal_purchases)

class DealPurchase extends AppModel {
    var $name = "DealPurchase"; 
    var $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id'
        )
    );
    //validation
}

People are also looking for solutions to the problem: javascript - ajax get json function doesn't work

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.