views:

248

answers:

1

I'm trying to update a record using the ORM library built in to Kohana 2.3.4. I'm basically modifying the script I use to insert the record in the first place. My problem is the record is getting inserted again, not updated. Here's my script:

     public function edit($id)
  {
   // Find the selected blog entry
   $blog = ORM::factory('article')->where('id',$id)->find();

            //other code to view data from $blog

   // Write the changes to the db for this id
   $title = $this->input->post('title');
   $content = $this->input->post('text_content');

   if(!empty($title) && !empty($content))
     {

   $edit_blog = ORM::factory('article')->where('id',$id);
   $edit_blog->title = $title;
   $edit_blog->content = $content;

   if($edit_blog->save())
    {
     url::redirect('admin/dashboard/blog/manage');
    }
              }

I've looked over the documentation Kohana provides, but I can't find an example of updating records. I thought that the $id argument passed in to the edit method would select a record that exists already and update it, but it just inserts a new one. Any Help? thanks!

+1  A: 

Hi

It's seems that you've forgotten to append the find() method while creating your $edit_blog object. By the way, there's no need to create another one, you can reuse the blog object you've instanciated in first place (here using a sligthly shorten syntax) :

public function edit($id)
            {
                    // Find the selected blog entry
                    $blog = new Article_Model($id);

        //other code to view data from $blog

                    // Write the changes to the db for this id
                    $title = $this->input->post('title');
                    $content = $this->input->post('text_content');

                    if(!empty($title) && !empty($content))
                      {

                    $blog->title = $title;
                    $blog->content = $content;

                    if($blog->save())
                            {
                                    url::redirect('admin/dashboard/blog/manage');
                            }
          }

Also you should consider to use the validation library inside your model.

Delapouite