framework icon indicating copy to clipboard operation
framework copied to clipboard

关于hasManyThrough的问题

Open surest-sky opened this issue 5 years ago • 0 comments

首先,我的配置文件

// 数据库表前缀
    'prefix'          => 'd88_',

在使用hasManyThrough的过程中

return $this->hasManyThrough(Role::class, ModelHasRole::class, 'user_id', 'id', $this->pk);

....
class ModelHasRole extends BaseModel
{
    public $pk = 'role_id';

    protected $table = 'model_has_role';
    ....

ModelHasRole 中定义了table, 但是得到的结果为 ** d88_model_has_role**

我追溯了源码到, Query.php -> 851 行 -> getJoinTable() 中的 932 行, 发现传入的 $join 会被

if ($prefix && false === strpos($table, '.') 
   && 0 !== strpos($table, $prefix) 
    && 0 !== strpos($table, '__')) 
{
         $table = $this->getTable($table);
}

这一行代码改变, 从而结果变成了 d88_model_has_role 设置的$table 基本是失效了

surest-sky avatar Jul 11 '19 08:07 surest-sky