Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to update my data in the save() method
  • After creating a new object I directly want to store all new data as XML in my database in an extra field. I know how to do that.

    The problem now is, that I need to update the just added object directly in the save step.

    I tried to do this like:

        public function action_create()
        {
            if (Input::method() == 'POST')
            {
                $val = Model_Animal::validate('create');
               
                if ($val->run())
                {
                    #Debug::dump($animal);
                    $animal = Model_Animal::forge(array(
                        'name' => Input::post('name'),
                        'age' => Input::post('age'),
                    ));

                    if ($animal and $animal->save())
                    {
                        // Extending Model with XML-Data
                        $animal = Model_Animal::forge($animal->to_array())->set(array(
                            'xml' => Format::forge($animal)->to_xml(),
                        ));   
                        $animal->save();

                        Session::set_flash('success', 'Added animal #'.$animal->id.'.');
                        Response::redirect('animal');
                    }
                    else
                    {
                        Session::set_flash('error', 'Could not save animal.');
                    }
                }
                else
                {
                    Session::set_flash('error', $val->error());
                }
            }
            $this->theme->set_partial('content', 'animal/create');   
        }

    but then I receive an error, that I try to add an object with the same ID, which is correct. As I cannot do something like update() there I have no idea how to handle that.

    Can someone please let me know, how can this be done? I read about observers but don't know how to handle them. Also I tried to do this in post_save() in the model without any luck.

    Thanks
    Kay
  • I'm not sure what you're trying to do here. Why would you forge a new object if you want to update an existing one? Any why save an object, and then create a new one in which you save an xml version of itself? It all looks very complicated an unneeded.

    But why not simply use
    if ($animal and $animal->save())
    {
        // Extending Model with XML-Data
        $animal->xml = Format::forge($animal)->to_xml();
        $animal->save();
    }
  • Thanks Harro.
    Sometimes it can be so simple :-)
  • One more question...

    I need the data in the XMl in the order which the fiels have in the Model definition and I need to parse out the "old" XML date. What I do now is:

            $animal = Model_Animal::find($id);
            $animal_array = $animal->to_array();
            unset($animal_array['xml']);
            $animal = Model_Animal::forge($animal_array);   
            $animal->xml = Format::forge($animal)->to_xml();
            $animal->save();

    When I dump  $animal now has exactly the data I need. :-)

    But why does the system tries to make a DB insert instead of an update?

    As per documentation, if I find the Model, change it and Save it, it should use update and not insert.
    Can I "force" that somehow?

    Thanks
    Kay
  • Because that 4th line overwrites the fetched object with a new one?

    If it is your intention to update the values of the object fetched with the find(), then do so, and don't forge a new object.

    $animal->from_array($animal_array);

    would do that.
  • Cool, thanks, from_array() was not in the doku, so I had no knowledge if it. :-)
  • I don't think any of the methods are properly documented, that's still on my todo list...

Howdy, Stranger!

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

In this Discussion