I have these models : Country,State,City,User with relations : Country hasMany State, State hasMany City, City hasMany User,
State belongsTo Country, City belongsTo State, User belongsTo City,
Now I want to fetch all the users with not only their cities but also countries and states. How can I do this?
Controller users/index:
public function index() {
$this->User->Behaviors->load('Containable');
$this->paginate = array('contain'=>array('State'));
debug($this->Paginator->paginate());exit;
$this->set('users', $this->Paginator->paginate());
}
output :
Warning (512): Model "User" is not associated with model "State" [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 342]
\app\Controller\UsersController.php (line 27)
array(
(int) 0 => array(
'User' => array(
'id' => '1',
'name' => 'username',
'city_id' => '1'
)
)
)
Model Country :
public $hasMany = array(
'State' => array(
'className' => 'State',
'foreignKey' => 'country_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
Model State :
public $belongsTo = array(
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'City' => array(
'className' => 'City',
'foreignKey' => 'state_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
Model City :
public $belongsTo = array(
'State' => array(
'className' => 'State',
'foreignKey' => 'state_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'city_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
Model User :
public $belongsTo = array(
'City' => array(
'className' => 'City',
'foreignKey' => 'city_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);