cakephp 2.x delete not working

445 Views Asked by At

im trying to delete a record with following code:

in Controller:

    public function delete($id = null) {
    if (!$this->Modelname->exists($id)) {
        throw new NotFoundException(__('Modelname ungültig'));
    }

    $this->Modelname->id = $id;

    $this->request->onlyAllow('post', 'delete');
    if ($this->Modelname->delete()) {
        $this->Session->setFlash(__('Modelname wurde gelöscht.'));
        return $this->redirect(array('controller' => 'modelnames', 'action' => 'index'));
    }
    $this->Session->setFlash(__('Modelname wurde nicht gelöscht.'));
}

in View:

<?= $this->Form->postLink(__('Delete'), array('controller' => 'modelnames', 'action' => 'delete', $id), array('confirm' => 'Soll dieser Eintrag wirklich gelöscht werden?')) ?>

Usually this works for me, but now its just refreshing the page without doing anything.. mhh any ideas?

2

There are 2 best solutions below

2
Manohar Khadka On

It looks like onlyAllow method is deprecated on the CakePHP version you are going through.

postLink is working well as it is generating equivalent html for post method.

Well if you need to allow only particular request (post,delete) there you can do following either:

if ($this->request->is('post')) { // if this is a post request
   if (!$this->Modelname->exists($id)) {
        throw new NotFoundException(__('Modelname ungültig'));
    }

    $this->Modelname->id = $id;

    if ($this->Modelname->delete()) {
        $this->Session->setFlash(__('Modelname wurde gelöscht.'));
        return $this->redirect(array('controller' => 'modelnames', 'action' => 'index'));
    }
    $this->Session->setFlash(__('Modelname wurde nicht gelöscht.'));
}
0
Donny Doe On

The answer is simple as dumb..

TL;DR version:
Form in Form.... (not working)

long version:
i got a huge view page to work over, where there has been kind of a small formwrapper around a table, which i didnt recognize while debugging that stuff.. (simple select table row - doing stuff with them thing) in this table we had to push extra placeholderdata which can be deleted at a specific time.. the delete via cake postLink (which generates a form) didnt work as you know.. and you know the rest of the story

sorry for wasting your time, but thanks for the fast helpattempt ! and thanks for the "onlyAllow" tipp :)