Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
PUT REST Controller + ORM unlimited fields
  • Hi guys,
    I wanted to share my code to make an update in the database without having to specify all the fields we want to update.
    The idea is to get the data in an array, and then generate the attributes to the object you want to Update.
    Then if an attribute doesn't exist on the database as a field we can catch the exception that is thrown bye FUEL.
    If there's something wrong with the code or you think you can improve it, please be my guest, it's a work in progress.
    Cheers!
    function put_user()
        {
            $user_id = input::put('user_id');
            $data = input::put('user'); //it's an array
            try {
                try {
                    $user = Model_user::find($user_id);
                    if (!$user) {
                        $this->response(NULL,404);
                    } else {
                        //attributes are added to the object
                        foreach ($data as $property=>$value) {
                            $user->$property = $value;
                        }
                        $user->save();
                    }
                } catch (Orm\ValidationFailed $e) {
                    print_r("validation");           
                }
            } catch (OutOfBoundsException $e) { 
                print_r("out_of_bounds");
            }
        }
    

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!