l3nox
l3nox

Reputation: 15

Cakephp - Cannot commit transaction - rollback()

I have a little problem. I have a Cakephp 3.6 project. All work fine, but when I want to delete a record in one controller show my a error.

Cannot commit transaction - rollback() has been already called in the nested transaction Cake\Database\Exception\NestedTransactionRollbackException

Cake\ORM\Table->delete
APP/Controller\NewsController.php, line 131

This is my delete action in NewsController.php

public function delete($id = null)
{
    $this->request->allowMethod(['post', 'delete']);
    $news = $this->News->get($id);
    if ($this->News->delete($news)) {
        $this->Flash->success(__('The news has been deleted.'));
    } else {
        $this->Flash->error(__('The news could not be deleted. Please, try again.'));
    }

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

And the error is highlighted on if ($this->News->delete($news)) {

What can I do ?

Upvotes: 1

Views: 3017

Answers (1)

502_Geek
502_Geek

Reputation: 2126

By default all deletes happen within a transaction. How about you disable the transaction with the atomic?

Something likes

$this->News->delete($news, ['atomic' => false]);

Upvotes: 8

Related Questions