java '的雄辩方法getKey()返回UUID的第一个数字


Laravel's Eloquent method getKey() returns the first number of the UUID

我有一个UserRole模型扩展了Catalyst的Sentinel包。

如果我使用默认的递增主键,一切都可以正常工作。当我将其更改为使用uid时,它的行为很奇怪。一切都很好,除了一点细节。

下面是检查User是否属于Role的代码片段:

public function inRole($role)
{
    if ($role instanceof RoleInterface) {
        $roleId = $role->getRoleId();
    }
    foreach ($this->roles as $instance) {
        if ($role instanceof RoleInterface) {
            if ($instance->getRoleId() === $roleId) {
                return true;
            }
        } else {
            if ($instance->getRoleId() == $role || $instance->getRoleSlug() == $role) {
                return true;
            }
        }
    }
    return false;
}

这段代码的作用是:如果我传递一个Role的实例,它循环遍历所有用户的角色,并检查是否有任何角色与提供的$role的id匹配。

$user->roles ($this->roles)的关系:

public function roles()
{
    return $this->belongsToMany(static::$rolesModel, 'role_users', 'user_id', 'role_id')->withTimestamps();
}
$role->getRoleId()正确返回UUID: "8fbc99e2-3cbb-4e98-b0e3-4c88027d787f"

但是$instance->getRoleId()返回8,或者UUID的第一个数字字符。如果第一个字符是字母,则返回0

我已经确认$instance确实是RoleInterface的一个实例,所以我认为它的行为应该与$role完全相同。

我还将public $incrementing = false添加到两个模型中。

我在所有被调用的供应商函数中做了几个检查点,但我不明白为什么会发生这种情况。

感谢您的帮助。

我在他们的GitHub存储库上打开了一个新问题,所以这里是保持答案同步的链接:github.com/cartalyst/sentinel/issues/289(对不起,我不能发布超过2个链接)

这是软件包中的一个bug,已经修复:https://github.com/cartalyst/sentinel/commit/239fa9ec88ce8fb955f5c0d85311d55a2f76e314

这个bug已经修复了,但是如果你有非常旧的laravel版本,它可以帮助我将这段代码添加到模型

public function getKeyName() {
    if (isset($this->id)) {
        return 'id_'; // any non-existed column  
    } else {
        return 'id'; // real id column name 
    }
}
public function getKey() {
    return $this->id;
}