在 Yii 2.0 中,一张关联表中的一个字段分别被两张表同时关联的实现
1、栏目配置:pa_config_column,如图1
2、栏目人员配置:pa_config_column_user,如图2
3、选题:pa_plan,如图3
4、选题任务:pa_plan_task,如图4
5、任务管理 – 我的任务,查询规则
(1)任务的租户ID为当前登录用户的租户ID;
(2)栏目配置未删除;
(3)选题未删除;
(4)选题任务未删除;
(5)选题任务的执行用户ID(`pa_plan_task`.`exec_user_id`)为当前登录用户ID && 栏目人员配置未删除 || 选题创建用户ID(`pa_plan`.`create_user_id`)为当前登录用户ID && 栏目人员配置未删除 || (栏目人员配置租户ID为当前登录用户租户ID && 栏目人员配置角色标识包含栏目负责人标识 && && 栏目人员配置未删除);
6、现在的查询规则,需求在于,主表为 选题任务:pa_plan_task,其需要关联表 选题:pa_plan;选题任务:pa_plan_task 需要关联表 栏目人员配置:pa_config_column_user,基于 `pa_plan_task`.`exec_user_id`;选题:pa_plan 需要关联表 栏目人员配置:pa_config_column_user,基于 `pa_plan`.`create_user_id`
7、\common\logics\PlanTask.php,声明关联关系
public function getConfigColumn() { return $this->hasOne(ConfigColumn::className(), [ 'id' => 'config_column_id' ]); } public function getPlanById() { return $this->hasOne(Plan::className(), [ 'id' => 'plan_id' ]); } public function getConfigColumnUser() { return $this->hasOne(ConfigColumnUser::className(), [ 'config_column_id' => 'config_column_id', 'user_id' => 'exec_user_id' ]); }
8、\common\logics\Plan.php,声明关联关系
public function getConfigColumn() { return $this->hasOne(ConfigColumn::className(), [ 'id' => 'config_column_id' ]); } public function getConfigColumnUser() { return $this->hasOne(ConfigColumnUser::className(), [ 'config_column_id' => 'config_column_id', 'user_id' => 'create_user_id' ]); }
9、查询代码最终如下,如果一张关联表中的一个字段分别被两张表同时关联,需要设置关联表的别名
$query = $modelClass::find() ->joinWith('configColumn') ->joinWith(['configColumnUser ccue']) ->joinWith(['planById' => function($q) { $q->joinWith('configColumnUser ccuc'); }]) ->where([ 'and', [$modelClass::tableName() . '.group_id' => $identity->group_id], [ConfigColumn::tableName() . '.is_deleted' => ConfigColumn::IS_DELETED_NO], [Plan::tableName() . '.is_deleted' => Plan::IS_DELETED_NO], [$modelClass::tableName() . '.is_deleted' => $modelClass::IS_DELETED_NO] ]) ->andWhere([ 'or', [ 'and', [$modelClass::tableName() . '.exec_user_id' => $identity->id], ['ccue.is_deleted' => ConfigColumnUser::IS_DELETED_NO] ], [ 'and', [Plan::tableName() . '.create_user_id' => $identity->id], ['ccuc.is_deleted' => ConfigColumnUser::IS_DELETED_NO] ], [ 'in', Plan::tableName() . '.config_column_id', $configColumnIds ] ]) ->orderBy([$modelClass::tableName() . '.sort_order' => SORT_ASC, $modelClass::tableName() . '.id' => SORT_DESC]);
10、最终生成的 SQL 语句如下:
SELECT `pa_plan_task`.* FROM `pa_plan_task` LEFT JOIN `pa_config_column` ON `pa_plan_task`.`config_column_id` = `pa_config_column`.`id` LEFT JOIN `pa_config_column_user` `ccue` ON `pa_plan_task`.`config_column_id` = `ccue`.`config_column_id` AND `pa_plan_task`.`exec_user_id` = `ccue`.`user_id` LEFT JOIN `pa_plan` ON `pa_plan_task`.`plan_id` = `pa_plan`.`id` LEFT JOIN `pa_config_column_user` `ccuc` ON `pa_plan`.`config_column_id` = `ccuc`.`config_column_id` AND `pa_plan`.`create_user_id` = `ccuc`.`user_id` WHERE (`pa_plan_task`.`group_id`='015ce30b116ce86058fa6ab4fea4ac63') AND (`pa_config_column`.`is_deleted`=0) AND (`pa_plan`.`is_deleted`=0) AND (`pa_plan_task`.`is_deleted`=0) AND (((`pa_plan_task`.`exec_user_id`='8') AND (`ccue`.`is_deleted`=0)) OR ((`pa_plan`.`create_user_id`='8') AND (`ccuc`.`is_deleted`=0)) OR (`pa_plan`.`config_column_id`=1)) ORDER BY `pa_plan_task`.`sort_order`, `pa_plan_task`.`id` DESC LIMIT 10
近期评论