thecodedeveloper.com
thecodedeveloper.com

Reputation: 3240

Get data based on other model id Cake php

ServiceController.php

public function admin_index() {
        $services=$this->Service->find('all', array('order'=>'Service.id desc'));
        $this->set('services', $services);
    }

admin_index.ctp(view file)

pr($services)
Array
(
    [Service] => Array
        (
            [id] => 53
            [user_id] => 65
            [first_name] => client
        )
    [User] => Array
            (
                [id] => 65
                [user_group_id] => 4
            )
    [SolutionType] => Array
            (
                [id] => 6
                [solution_type] => face to face interaction
                [status] => 0
            )

    [Service] => Array
        (
            [id] => 54
            [user_id] => 66
            [first_name] => client
        )
    [User] => Array
            (
                [id] => 66
                [user_group_id] => 5
            )
    [SolutionType] => Array
            (
                [id] => 6
                [solution_type] => face to face interaction
                [status] => 0
            )       
)

Service.php(model file)

public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'SolutionType' => array(
            'className' => 'SolutionType',
            'foreignKey' => 'solution_type_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
            )
        )

I want to make condition in ServicesController show all data where User user_group_id = 5.

Upvotes: 0

Views: 325

Answers (1)

Yogesh Saroya
Yogesh Saroya

Reputation: 1515

Try this

 $services=$this->Service->find('all', array('conditions'=>array('User.user_group_id'=>5),
                                             'order'=> array('Service.id' => 'Desc')));

Upvotes: 5

Related Questions