yii2 中间关联表 via() viaTable()
Yii2 中间关联表 via() viaTable()
viaTable()
/** * @link https://www.yiiframework.com/doc/guide/2.0/zh-cn/db-active-record#junction-table * user 表 id * book_user 表 user_id book_id * book 表 id * * #SELECT * FROM `book` WHERE `id`='1' * $book = Book::findOne(1) * * #SELECT * FROM `book_user` WHERE `book_id`=1 * #SELECT * FROM `user` WHERE `id` IN ('1', '2',....) * $model->bookUserItems; * * @return \yii\db\ActiveQuery */ public function getBookUserItems() { return $this->hasMany(User::className(), ['id' => 'user_id']) ->viaTable('book_user',['book_id'=>'id']); }
官方地址: 中间关联表(Relations via a Junction Table)
其他: 文档地址