我是从yii开始的.
我有以下数据库结构:
Table: Rows:
user [id,login,password,name,email]
userToProject [user_id,project_id,role]
project [id,name,status]
Run Code Online (Sandbox Code Playgroud)
我想检索在status = 3的项目中工作的所有用户作为role = manager.顺便说一句,这是我的代码,我需要进行第二次连接才能达到项目状态.
$criteria=new CDbCriteria;
$criteria->join='INNER JOIN {{userToProject}} a ON t.id=a.user_id and a.role='.Role::MANAGER;
$criteria->distinct=true;
return User::model()->findAll($criteria);
Run Code Online (Sandbox Code Playgroud)
我可以使用标准制作它还是应该实现SQL命令并运行它?
谢谢
你在userToProject模型中建立这样的关系......
'user'=>array(self::BELONGS_TO, 'User', 'user_id')
// after belongs_to User is a model class name..
'project'=>array(self::BELONGS_TO, 'Project', 'project_id'),
// after belongs_to Project is a model class name..
Run Code Online (Sandbox Code Playgroud)
然后使用像...这样的活动记录
$allrecord = UserToProject::model()->with('user','project')->findAll(
'status = :status AND role = :role',
array(':status' => 3 , 'role' => 'manager'));
//in with bracket user , project is your relation name..
Run Code Online (Sandbox Code Playgroud)
现在在$ allrecord中你有阵列形式的所有记录......