質問

I'm running though the Laravel 4 Ardent package found here: https://github.com/laravelbook/ardent

I'm trying to integrate the code found in Ardent's README within my User Model:

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

    return true;
}

My User Model Test:

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");
}

When I test via PhpUnit, it is telling me that '$this->changed()' is undefined.

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

I'm basically trying to do as the tutorial says, and check to make sure the password has changed before saving it to the DB. Any help would be greatly appreciated.

役に立ちましたか?

解決 2

It doesn't appear that Ardent has a 'changed()' method as described in it's documentation. There is a boolean variable you can set to automatically HASH the password on save().

Automatically Transform Secure-Text Attributes

他のヒント

I don't use Ardent, but this can be done through Laravel directly with isDirty

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

I don't see any changed method (or it being some pseudo-method picked up by __call) anywhere.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top