我有这些模型: 国家,州,城市, 有关系的用户: 国家有很多州,州有很多城市,城市有很多用户,
国家属于国家,城市属于国家,用户属于城市,
现在我想获取所有用户,不仅包括他们的城市,还包括国家和州。我怎样才能做到这一点?
控制器 用户/索引:
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());
}
输出:
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'
)
)
)
示范国家:
public $hasMany = array(
'State' => array(
'className' => 'State',
'foreignKey' => 'country_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
模型状态:
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' => ''
)
);
模范城市:
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' => ''
)
);
模型用户:
public $belongsTo = array(
'City' => array(
'className' => 'City',
'foreignKey' => 'city_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);