如何在 cakephp 中使用事务?

Posted

技术标签:

【中文标题】如何在 cakephp 中使用事务?【英文标题】:How to use transactions in cakephp? 【发布时间】: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'));
    

【问题讨论】:

【参考方案1】:

你可以试试下面的代码:

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();

【讨论】:

第二次保存呢?问题是我想保证两个实体的保存:$this->Users->save($user) and $clientTable->save($client) 感谢您的回答,您的回答让我找到了方向!【参考方案2】:

我认为我能够以“事务性”进行交易。

在我的测试中,如果 Clients 实体有错误,则不会保存 Users 实体。

我不知道这是否是最好的方法,但它确实有效

public function register()
    
        $locator = TableRegistry::getTableLocator();
        $userTable = $locator->get("Users");
        $clientTable = $locator->get("Clients");

        $user = $userTable->newEntity();
        $client = $clientTable->newEntity();
        if ($this->request->is('post')) 
            $request = $this->request->getData();

            $user = $userTable->patchEntity($user, $request);

            $conn = ConnectionManager::get('default');

            try 
                $conn->transactional(function ($conn) use ($userTable, $clientTable, $user, $client, $request) 

                    $userTable->saveOrFail($user, [
                        'atomic' => false
                    ]);
                    $request['user_id'] = $user->id;
                    $client = $clientTable->patchEntity($client, $request);

                    $clientTable->saveOrFail($client, [
                        'atomic' => false
                    ]);
                );
                $this->Flash->success(__('The user has been registered.'));

                return $this->redirect([
                    'action' => 'login'
                ]);
             catch (\Cake\ORM\Exception\PersistenceFailedException $e) 
                $this->Flash->error(__('Error on save: 0', $e->getMessage()));

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

            $this->Flash->error(__('The user could not be registered. Please, try again.'));
        
        $this->set(compact('$user'));
    

【讨论】:

以上是关于如何在 cakephp 中使用事务?的主要内容,如果未能解决你的问题,请参考以下文章

ORM如何在CakePHP3中运行

如何在 cakephp 2.10 中使用 JWT?

如何在 cakephp4 中使用 Admad cakephp-glide 删除 Glide 缓存?

如何在 CakePHP 中使用自定义 MySQL 查询?

如何在 nginx 中配置 cakephp

如何在 CakePhp 组件中调用回调?