Kohana 3.2 ORM 插入一行而不是更新

发布于 2025-01-05 02:04:25 字数 1637 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(1

葬花如无物 2025-01-12 02:04:25

问题似乎是您没有将 ID 传递给表单中的控制器。

您的表单操作应类似于

action="/photoalbum/edit/ALBUM_ID"

或类似内容。

您正在请求相册 ID,

$id = $this->request->param('id');
$album = ORM::factory('photoalbum', $id);

但您的表单操作中没有参数“id”。

<form action="/photoalbum/edit/" method="POST">

或者,您可以将 ID 作为表单中的隐藏字段发送,但随后您必须使用

$id = $this->request->post('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

$id = $this->request->param('id');
$album = ORM::factory('photoalbum', $id);

But you have no parameter "id" in you Forms action.

<form action="/photoalbum/edit/" method="POST">

Or, you could send the ID as hidden field in you form, but then you have to get it with

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