Kohana v3.1.0 ORM _ignored_columns — now that it's gone, what should I do instead?

百般思念 提交于 2019-12-22 08:52:56

问题


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.


回答1:


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'));


来源:https://stackoverflow.com/questions/4982570/kohana-v3-1-0-orm-ignored-columns-now-that-its-gone-what-should-i-do-inste

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!