Laravel 4 Ardent beforeSave()和$ this-> changed()

时间:2013-06-06 15:59:18

标签: laravel laravel-4 ardent

虽然我在这里找到了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()

我基本上是按照教程所说的那样尝试,并检查以确保密码已更改,然后再将其保存到数据库中。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

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

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

我没有看到任何改变的方法(或者是__call选择的一些伪方法)。

答案 1 :(得分:1)

Ardent似乎没有'{(3}}中描述的'changed()'方法。有一个布尔变量可以设置为在save()上自动HASH密码。

documentation