【发布时间】:2019-11-01 16:04:33
【问题描述】:
我正在尝试将 commit 与 setConnection() 一起使用 但它不起作用...... 我不知道如何在 cakephp 中进行交易
我在文档中发现: https://book.cakephp.org/3.0/en/orm/database-basics.html#using-transactions
但我无法实现... 问题是我想保证两个实体的保存:
$this->Users->save($user) and $clientTable->save($client)
这是我的代码:
public function register()
{
$locator = TableRegistry::getTableLocator();
$clientTable = $locator->get("Clients");
$user = $this->Users->newEntity();
$client = $clientTable->newEntity();
if ($this->request->is('post')) {
$request = $this->request->getData();
$user = $this->Users->patchEntity($user, $request);
$result = false;
// begin()
if ($this->Users->save($user)) {
$request['user_id'] = $user->id;
$client = $clientTable->patchEntity($client, $request);
if ($clientTable->save($client)) {
$result = true;
}
}
if ($result) {
// commit()
$this->Flash->success(__('The user has been registered.'));
return $this->redirect([
'action' => 'login'
]);
} else {
// rollback()
}
$this->Flash->error(__('The user could not be registered. Please, try again.'));
}
$this->set(compact('$user'));
}
【问题讨论】:
标签: php cakephp transactions cakephp-3.0 commit