【问题标题】:Laravel 4 Ardent beforeSave() and $this->changed()Laravel 4 Ardent beforeSave() 和 $this->changed()
【发布时间】:2013-10-07 20:03:59
【问题描述】:

我正在运行这里找到的 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()

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

【问题讨论】:

    标签: laravel laravel-4 ardent


    【解决方案1】:

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

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

    我在任何地方都没有看到任何更改的方法(或者它是 __call 采用的一些伪方法)。

    【讨论】:

    • 感谢您的回答。你说得对,改变的方法甚至不是 Ardent 包的一部分。我认为作者没有说清楚有点奇怪……我发现 Ardent 确实带有自动密码哈希。 $autoHashPasswordAttributes
    【解决方案2】:

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

    Automatically Transform Secure-Text Attributes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-08
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 2013-08-03
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多