0

我正在运行这里找到的 Laravel 4 Ardent 包: https ://github.com/laravelbook/ardent

我正在尝试将 Ardent 的 README 中的代码集成到我的用户模型中:

public function beforeSave( $forced )
{
    // if there's a new password, hash it
    if($this->changed('password'))
    {
        $this->password = Hash::make($this->password);
    }

    return true;
}

我的用户模型测试:

public function testSetPassword() 
{
    // Create a new User
    $user = new User;
    $user->email = "joe@smalls.com";
    $user->password = "password";
    $user->password_confirmation = "password";
    $user->save();

    /* Test #1 - Test to make sure password is being hashed */
    $this->assertTrue(Hash::check('password', $user->password), "the set_password() function did not return TRUE");
}

当我通过 PhpUnit 进行测试时,它告诉我 '$this->changed()' 是未定义的。

BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::changed()

我基本上是按照教程所说的那样做,并在将密码保存到数据库之前检查以确保密码已更改。任何帮助将不胜感激。

4

2 回答 2

1

如其文档中所述,Ardent 似乎没有“changed()”方法。有一个布尔变量可以设置为在 save() 上自动对密码进行哈希处理。

自动转换安全文本属性

于 2013-06-07T03:26:44.343 回答
1

我不使用 Ardent,但这可以通过 Laravel 直接使用isDirty

if ($this->isDirty('password')) {
  //...
}

我在任何地方都看不到任何更改的方法(或者它是由 拾取的一些伪方法__call)。

于 2013-06-06T19:36:07.610 回答