Php 有多个选择不起作用
您好,我正在尝试从HasMany中的多个表中选择数据,但它不起作用。这是我的密码Php 有多个选择不起作用,php,mysql,cakephp,Php,Mysql,Cakephp,您好,我正在尝试从HasMany中的多个表中选择数据,但它不起作用。这是我的密码 class UserInfo extends AppModel { public $useTable = 'user_info'; public $primaryKey = 'user_id'; public $belongsTo = array( 'User' => array( 'className' => 'User',
class UserInfo extends AppModel
{
public $useTable = 'user_info';
public $primaryKey = 'user_id';
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'type' => 'RIGHT',
'fields' => array('User.user_id','User.email','User.active')
)
);
public $hasMany = array(
'UserCategory' => array(
'className' => 'UserCategory',
'foreignKey' => 'user_id',
'dependent' => true,
'conditions' => array('user_id = UserCategory.user_id')
//'order' => 'UserCategory. DESC'
)
);
public function getUserDetails($user_id){
$this->Behaviors->attach('Containable');
return $this->find('all', array(
'conditions' => array(
'UserInfo.user_id' => $user_id
),
'contain' => array(
'User', 'UserCategory.Category',
)
));
}
如果我试图从另一个表中获取数据,其中包含user\u id
,如下所示
public $hasMany = array(
'UserCategory' => array(
'className' => 'UserCategory',
'foreignKey' => 'user_id',
'dependent' => true,
'conditions' => array('user_id = UserCategory.user_id')
//'order' => 'UserCategory. DESC'
),
'Skill' => array(
'className' => 'Skill',
'foreignKey' => 'user_id',
'dependent' => true
)
);
它卖不出去。当我运行sql转储时。它根本不运行查询。因此,这意味着我无法从技能表中获取数据我建议您彻底阅读cakephp 2中的文档。总之,当您使用containable时,例如:
'contain' => array(
'User', 'UserCategory.Category',
)
您可以在其中添加所需的模型,就像在您的案例中一样,这将帮助您:
'contain' => array(
'User', 'UserCategory.Category', 'Skill'
)
Containeable有助于根据需要筛选find()数据