Kohana 2.3.4 ORM更新问题

发布于 2024-08-14 20:47:10 字数 993 浏览 1 评论 0原文

我正在尝试使用 Kohana 2.3.4 内置的 ORM 库更新记录。我基本上是在修改最初用来插入记录的脚本。我的问题是记录被再次插入,而不是更新。这是我的脚本:

        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');
                }
              }

我查看了 Kohana 提供的文档,但找不到更新记录的示例。我认为传递给编辑方法的 $id 参数会选择一条已经存在的记录并更新它,但它只是插入一条新记录。有帮助吗?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总以为 2024-08-21 20:47:10

您似乎忘记在创建 $edit_blog 对象时附加 find() 方法。
顺便说一句,不需要创建另一个,您可以重用您首先实例化的博客对象(此处使用稍微缩短的语法):

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');
                            }
          }

您还应该考虑在模型中使用验证库。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文