Yii2 关联查询 joinWith、with实践

51 阅读1分钟
joinWith 方法
// 小说章节信息查询
        $chapterModel = Chapter::find()
            ->alias('l')
            ->select([
                'l.title', 'l.chapter_no', 'l.content','l.novel_id'
            ])
            ->where(['l.id' => $chapter_id])
            ->joinWith(['novel' => 
                function ($query) {
                      $query->select(['title','id']);
                }
            ])
            // ->asArray()
            ->one();
with 方法 不能跟select 共用
    $member = $this->modelClass::find()
            ->where(['id' => $member_id])
            ->andFilterWhere(['in', 'id', $ids])
            ->andWhere(['>=', 'status', StatusEnum::DISABLED])
            ->with(['account', 'merchant'])
            ->asArray()
            ->one();