TP6.0.3模型类多对多关联

浏览:1024 发布日期:2020/06/28 分类:ThinkPHP6专区
class User{
    public function books(){
        return $this->belongsToMany(Book:class, UserBook::class)
            ->wherePivot('status', 1)->order(['book.id']);
    }
}
UserBook类继承了think\model\Pivot

belongsToMany()->wherePivot()->order()的写法,
查询$user = User::with(['books'])->find(1);在TP6.0.2能正常运行
升级到6.0.3之后,报错[think\db\exception\PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'pivot'
问题是,这是bug还是属于一个错误用法?

另外,日志里的SQL,只记录了错误前一条SQL
错误的那一条SQL却没有记录,不利于查bug$user = User::fetchSql(true)->with(['books'])->find(1);也只能打印外面的SQL
SELECT * FROM `user` WHERE `id` = 1 LIMIT 1

里面的with关联预加载的没有记录

进一步测试了
belongsToMany()->wherePivot()
的写法没有报错

belongsToMany()->wherePivot()->order()
的写法报错了
最佳答案
评论( 相关
后面还有条评论,点击查看>>