Dan Hanly
Dan Hanly

Reputation: 7839

CakePHP - When editing a record, sometimes we get a duplicate

Now, this doesn't happen all the time and my efforts to replicate the error have been hit and miss.

Basically, we have a job system which has a user, a title, body and a deadline. Sometimes, saving from the job's edit form produces a duplicate, i.e. the same job (with all the same content) but with a new id.

I was wondering if anybody has run into a similar error.

Here's my Job Controller's Edit Function:

function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid job', true));
        }
        if (!empty($this->data)) {
            if ($this->Job->save($this->data)) {
                $this->Session->setFlash(__('The job has been saved', true));
            } else {
                $this->Session->setFlash(__('The job could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Job->read(null, $id);
        }
        $users = $this->Job->User->find('list', array('fields' => array('User.username')));
        $clients = $this->Job->Client->find('list');
        $stages = $this->Job->Stage->find('list');
        $this->set(compact('users', 'clients', 'stages'));
    }

Any help is appreciated.

Upvotes: 0

Views: 517

Answers (1)

Paulo Rodrigues
Paulo Rodrigues

Reputation: 5303

Probably in your form you don't have the id of this Job. Make sure you have this in view form:

echo $this->Form->input('id');

Upvotes: 2

Related Questions