public function actionPost() { $update = Topic::updateAll(['last_comment_time' => new Expression('created_at')], ['and', ['type' => Topic::TYPE], ['or', ['last_comment_username' => ''], ['last_comment_username' => null]]]); $this->stdout("同步最后回复时间,同步{$update}条数据\n"); $subQuery = new Query(); $subQuery->from(PostComment::tableName())->where(['status' => PostComment::STATUS_ACTIVE])->orderBy(['created_at' => SORT_DESC]); $comment = PostComment::find()->from(['tmpA' => $subQuery])->groupBy('post_id')->all(); Topic::updateAll(['comment_count' => 0], ['type' => Topic::TYPE]); $updateComment = []; foreach ($comment as $value) { $commentCount = PostComment::find()->where(['post_id' => $value->post_id, 'status' => PostComment::STATUS_ACTIVE])->count(); $updateComment[] = Topic::updateAll(['last_comment_time' => $value->created_at, 'last_comment_username' => $value->user->username, 'comment_count' => $commentCount], ['id' => $value->post_id, 'type' => Topic::TYPE]); } $this->stdout("校正最后回复时间和回复会员还有评论条数,校正" . count($updateComment) . "条数据\n"); }