How can I use database transaction in cakephp3.4
I have tried as per below:
$this->User->begin();
$this->User->commit();
$this->User->rollback();
but it throws error as “Unknown method “begin””
How can I use database transaction in cakephp3.4
I have tried as per below:
$this->User->begin();
$this->User->commit();
$this->User->rollback();
but it throws error as “Unknown method “begin””
If you are using 3.4, $this->Users->save($userEntity), is wrapped in a transaction already. If you want to control the transaction you can
try {
$this->Users->getConnection()->begin();
$this->Users->saveOrFail($userEntity, [‘atomic’ => false]);
$this->Users->getConnection()->commit();
} catch(\Cake\ORM\Exception\PersistenceFailedException $e) {
$this->Users->getConnection()->rollback();
}
Another possibility is found in the book.
https://book.cakephp.org/3.0/en/orm/saving-data.html#converting-multiple-records
}
you can also use transactional which is more convinient
On top of same page that Graziel write is:
use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');
so, try using ->begin() on connection.