开发者

Kohana 2.3.4 ORM update question

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

         开发者_StackOverflow中文版   // 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜