Редактировать в CakePHP создает новую запись вместо обновления старой

Я пытался следовать учебнику по блогу CakePHP (http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/blog.html) и все работает нормально, кроме метода редактирования. Кажется, работает функция редактирования, как указано в учебнике, но вместо обновления старой записи она создает новую запись в базе данных. (Но говорит, что он обновил старую запись с сообщением «Jop был обновлен»)

Мои исходные файлы:

edit.ctp

<html>
<!-- File: /app/View/Jobs/edit.ctp -->
<h1>Edit Job</h1>

<?php
echo $this->Form->create('Job');
echo $this->Form->input('type');
echo $this->Form->input('name');
echo $this->Form->input('company');
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('Save Job');
?>
</html>

JobsController.php

class JobsController extends AppController
{
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');

public function index()
{
$this->set('jobs', $this->Job->find('all'));
}

public function view($id=null) {
if (!$id)
{
throw new NotFoundException(__('Invalid job'));
}

$job = $this->Job->findById($id);
if (!$job)
{
throw new NotFoundException(__('Invalid job'));
}
$this->set('job', $job);
}

public function add() {
if ($this->request->is('POST')) {
$this->Job->create();
if ($this->Job->save($this->request->data))
{
$this->Session->setFlash(__('Your job has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash('Unable to add your job.');
}
}

public function edit($id=null) {
if (!$id) {
throw new NotFoundException(__('Invalid job'));
}

$job = $this->Job->findById($id);

if (!$job) {
throw new NotFoundException(__('Invalid job'));
}

if ($this->request->is(array ('POST', 'PUT'))) {
$this->Job->id = $id;

if ($this->Job->save($this->request->data)) {
$this->Session->setFlash(__('Job has been updated.'));
return $this->redirect(array('action' => 'index'));
}
else {
$this->Session->setFlash(__('Unable to update the job.'));
}

if (!$this->request->data) {
$this->request->data = $job;
}

}
}

public function delete($id) {
if (!$id) {
throw new NotFoundException(__('Invalid job'));
}

if ($this->request->is('GET')) {
throw new MethodNotAllowedException();
}

if ($this->Job->delete($id)) {
$this->Session->setFlash(__('Job has been deleted.'));
}
else {
$this->Session->setFlash(__('Unable to delete job.'));
}

return $this->redirect(array('action' => 'index'));
}
}

?>

2

Решение

Если у кого-то еще есть эта проблема, это решение, которое работало для меня:

if (!$this->request->data) {
$this->request->data = $job;
}

было написано неправильно, это не должно быть в пределах

if ($this->request->is(array ('POST', 'PUT')))

блок, но на том же уровне, то есть на один слой с меньшим отступом.

1

Другие решения

Пожалуйста, проверьте свою страницу edit.ctp, вам не хватает некоторых.

<?php echo $this->Form->create('Job'); ?>

<?php
echo $this->Form->hidden('id', array('value' => $this->data['Job']['id'])); ?>
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector