user755157
user755157

Reputation: 3

Kohana 3.2 ORM inserts a row instead of updating

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>

Upvotes: 0

Views: 863

Answers (1)

Thorsten
Thorsten

Reputation: 5644

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

Upvotes: 1

Related Questions