简体   繁体   中英

Kohana - adding values to the query before ORM::Save

I am using the ORM to save the user registration data into the database. The code is as follows.

if ($_POST) {
    $user = Model::factory('member');
    $post = $user->validate_create($_POST); 
    if ($post->check()) {
        $user->values($post);
        $user->save();
        // redirect to create gallery.
    }
}

I have some values such as the UserType which is not part of the $_POST but has to be saved in the members table as part of the user registration. Is it a good idea to alter the values of $post and add UserType to it or is there any other recommended methods for achieving this?

It's ok to alter the $post since it's just an array with the values taken from request now.

You can do it either by editing the $post array:

$post['usertype'] = 'customer';
$user->values($post);
$user->save();

Or you can set the value to your ORM object directly:

$user->values($post);
$user->usertype = 'customer';
$user->save();

Both should be fine

You can use hidden inputs in your form. For example:

Form::hidden('usertype', 'customer');

If you don't want to change $_POST array. If you want to check is user sending $_POST request, use Kohana's method:

if($this->request->method() === Request::POST) {}

instead of:

if($_POST)

By the way, get a $_POST data in this way:

$post = $this->request->post();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM