Question

It seems that in v3.1.0 of Kohana's ORM that the _ignored_columns property has been removed.

What the is the recommended technique to dealing with fields that aren't in the databases? The case I have right now is password_confirm where password is a field, but we require the user to enter their password twice.

Was it helpful?

Solution

You can pass an extra validation object to save, create and update. So your example would look like:

/**
 * Password validation for plain passwords.
 * 
 * @param array $values
 * @return Validation
 */
public static function get_password_validation($values)
{
    return Validation::factory($values)
        ->label('password', 'password')
        ->label('password_confirm', 'repeat password')
        ->rule('password', 'not_empty')
        ->rule('password', 'min_length', array(':value', 8))
        ->rule('password_confirm', 'matches', array(':validation', ':field', 'password'));
}

/**
 * Create user account
 * 
 * @param array $values 
 * @param array $keys
 * @throws ORM_Validation_Exception
 */
public function create_user($values, $keys)
{
    $external = Model_User::get_password_validation($values);

    $this->values($values, $keys);
    return $this->create($external);
}

Notice how the password validation is passed into the create method.

The $keys value specifies which values should be saved into the model. "password_confirm" was not in that list so it is ignored. This feature is security related too, you wouldn't want users manually setting the id in their POST request.

Then you can create a user by calling create_user:

$user->create_user($_POST, array('username', 'email', 'password'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top