【问题标题】:How to use transactions in cakephp?如何在 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'));
    }

【问题讨论】:

    标签: php cakephp transactions cakephp-3.0 commit


    【解决方案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'));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多