public function test()
{
$userRole = M("user_role");
$table = $userRole //->alias('ur')
->join('smatrix_user ON smatrix_user.id = smatrix_user_role.userid')
->join('smatrix_role ON smatrix_role.id = smatrix_user_role.roleid')
->field('smatrix_user_role.*')->select();
dump($table);
}
【方法2】
class UserModel extends RelationModel{
protected $_li
'Role' => array(
'mapping_type' => self::MANY_TO_MANY,//关联关系:必须定义一张参照表;
'class_name' => 'Role', //要关联的模型,系统会定义到相关数据表
'foreign_key' => 'userid', //关联表的外键
'mapping_name' => 'roles',
'relation_foreign_key' => 'roleid',
'relation_table' => 'smatrix_user_role' ,//此处应显式定义中间表名称,且不能使用C函数读取表前缀
'as_fields' => 'username,rolename',
),
);
}
(控制器中使用)$user->relation(true)->find(1);
最佳答案