Kohana 2.3.4 ORM更新问题
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎忘记在创建 $edit_blog 对象时附加 find() 方法。
顺便说一句,不需要创建另一个,您可以重用您首先实例化的博客对象(此处使用稍微缩短的语法):
您还应该考虑在模型中使用验证库。
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) :
Also you should consider to use the validation library inside your model.