I have three table users
and user_categories
and categories
when create new user then user choose multiple category and selected categories save in user_categories with hashMany associations
How to get category details ?
In your User model put
class User extends AppModel {
public $hasAndBelongsToMany = array(
'Category' => array(
'joinTable' => 'user_categories',
)
);
}
then you can get user categories like this:
$this->User->find('all', ['contain' => ['Category']]);
There are below possibility to get the category detail on the user query giving by the appropriate relations:
// User.php
class User extends AppModel {
public $actsAs = array('Containable');
public $hasMany = array(
'UserCategory'
);
}
//UserCategory.php
class UserCategory extends AppModel {
public $actsAs = array('Containable');
public $belongsTo = array(
'Category'
);
}
//Category.php
class Category extends AppModel {
public $actsAs = array('Containable');
public $hasMany = array(
'UserCategory'
);
}
So, if you want category data along with user, your query will be similar to:
$this->User->find('all',['contain'=>['UserCategory'=>['Category']]]);
Hope this will solve your concern.