2

I tried to follow the CakePHP blog tutorial (http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/blog.html) and everything works fine, except for the edit method. The edit function as specified in the tutorial seems to work, but instead of updating the old entry, it creates a new entry in the database. (But says that it did update the old entry with the "Jop was updated" message)

My source files are:

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

?>
Kora K
  • 425
  • 1
  • 4
  • 15
  • Thanks for your help!! The ID is correct, although I can only display it with `die($id);` because otherwise the page is updated and the ID isn't displayed anymore on the new page. `$this->Job->id= $this->request->data['Job']['id'];` doesn't work either :/ Same behavior as before. – Kora K Apr 01 '16 at 18:09
  • Oh wow, that's odd! It displays `Array ( [Job] => Array ( [type] => hahah [name] => hahakak [company] => kahaahah [id] => ) )` I hope this formats right. Okay, so the ID _is_ empty. Why? – Kora K Apr 01 '16 at 18:19
  • Okay, so changing the order in _edit.ctp_ doesn't help. Printing job returns something like `Array ( [Job] => Array ( [id] => 38 [type] => Praktikum [name] => Einstein [company] => USGov [created] => 2016-04-01 17:41:05 [modified] => 2016-04-01 17:41:05 ) )`, this still looks okay to me... – Kora K Apr 01 '16 at 20:05
  • This echo alternative also doesn't work. You mean in MySQL? I created the table with basically this: http://book.cakephp.org/2.0/en/getting-started.html#creating-the-blog-database So `id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY`. The rest of my columns are varchars... – Kora K Apr 01 '16 at 21:19

2 Answers2

1

If someone else has this problem, this is the solution that worked for me:

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

was indented wrong, it shouldn't be within the

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

block, but on the same level, i.e. one layer less indented.

Kora K
  • 425
  • 1
  • 4
  • 15
0

Please check your edit.ctp page you are missing some.

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

   <?php
    echo $this->Form->hidden('id', array('value' => $this->data['Job']['id'])); ?>
Digpal Singh
  • 166
  • 1
  • 5
  • That doesn't work, the error message is `Notice (8): Undefined index: Job [APP/View/Jobs/edit.ctp, line 9]`. Referring to the second line of code that you posted. – Kora K Apr 03 '16 at 14:38