Kohana 3.2 ORM 插入一行而不是更新
SQL:
CREATE TABLE IF NOT EXISTS `photoalbums` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
模型:
<?php defined('SYSPATH') OR die('No Direct Script Access');
Class Model_PhotoAlbum extends ORM {}
?>
控制器的操作:
public function action_edit()
{
$id = $this->request->param('id');
$album = ORM::factory('photoalbum', $id);
$this->template->album = $album;
if ($this->request->post())
{
$album->title = $this->request->post('title');
$album->save();
$this->request->redirect('/');
}
}
每次我编辑某个专辑时,ORM 只是在数据库中插入新实例而不是更新它。我用谷歌搜索得很好,但找不到解决方案。 另外,我尝试使用 Jelly ORM,它也导致了这个问题。
更新:
树枝:
<!DOCTYPE HTML>
<html>
<head>
<title>Фотоальбомы</title>
<link rel="stylesheet" type="text/css" href="/media/css/main.css" />/>
<script src="media/js/jquery-1.7.1.min.js" />
<script src="media/js/main.js" />
</head>
<body>
<h1>Новый фотоальбом</h1>
<form action="/photoalbum/edit/" method="POST">
<p>
<label for="title">Название:</label><input type="text" id="title" name="title" value="{{ album.title }}" />
</p>
<p><input type="submit" value="Сохранить" /></p>
</form>
</body>
</html>
SQL:
CREATE TABLE IF NOT EXISTS `photoalbums` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
Model:
<?php defined('SYSPATH') OR die('No Direct Script Access');
Class Model_PhotoAlbum extends ORM {}
?>
Controller's action:
public function action_edit()
{
$id = $this->request->param('id');
$album = ORM::factory('photoalbum', $id);
$this->template->album = $album;
if ($this->request->post())
{
$album->title = $this->request->post('title');
$album->save();
$this->request->redirect('/');
}
}
Every time I edit some album, ORM just insers new instance in DB instead of update it. I googled very well, but i can't find the solution.
Also, i tried to use Jelly ORM and it caused this problem too.
UPDATE:
TWIG:
<!DOCTYPE HTML>
<html>
<head>
<title>Фотоальбомы</title>
<link rel="stylesheet" type="text/css" href="/media/css/main.css" />/>
<script src="media/js/jquery-1.7.1.min.js" />
<script src="media/js/main.js" />
</head>
<body>
<h1>Новый фотоальбом</h1>
<form action="/photoalbum/edit/" method="POST">
<p>
<label for="title">Название:</label><input type="text" id="title" name="title" value="{{ album.title }}" />
</p>
<p><input type="submit" value="Сохранить" /></p>
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎是您没有将 ID 传递给表单中的控制器。
您的表单操作应类似于
action="/photoalbum/edit/ALBUM_ID"
或类似内容。
您正在请求相册 ID,
但您的表单操作中没有参数“id”。
或者,您可以将 ID 作为表单中的隐藏字段发送,但随后您必须使用
The problem seems to be that you are not passing the ID to the controller in your form.
Your form action should look like
action="/photoalbum/edit/ALBUM_ID"
or something similar.
You are requesting the album ID with
But you have no parameter "id" in you Forms action.
Or, you could send the ID as hidden field in you form, but then you have to get it with