Starts a transaction.
public beginTransaction ( string | null $isolationLevel = null ) : |
||
$isolationLevel | string | null | The isolation level to use for this transaction. See [[Transaction::begin()]] for details. |
return | the transaction initiated |
/** * @throws \yii\base\InvalidConfigException * @throws \yii\db\Exception */ public function actionUpdate() { $assignments = $this->getAllAssignments(); $useTransaction = $this->authManager instanceof \yii\rbac\DbManager && $this->useTransaction === true; $transaction = null; if ($useTransaction) { $transaction = $this->db->beginTransaction(); } try { $this->authManager->removeAll(); $this->updateRules(); $this->updateRoles(); $this->updatePermission(); $this->updateInheritanceRoles(); $this->updateInheritancePermissions(); if (!empty($assignments)) { $this->restoreAssignments($assignments); } if ($transaction !== null) { $transaction->commit(); } } catch (\Exception $e) { $this->stderr($e->getMessage() . PHP_EOL); if ($transaction !== null) { $transaction->rollBack(); } } if ($this->authManager instanceof \yii\rbac\DbManager) { $this->authManager->invalidateCache(); } }
/** * Return next job from the queue. * @return Job|boolean the job or false if not found. */ protected function fetchJob() { //Avoiding multiple job. $transaction = $this->db->beginTransaction(); $row = $this->fetchLatestRow(); if ($row == false || !$this->flagRunningRow($row)) { $transaction->rollBack(); return false; } $transaction->commit(); $job = $this->deserialize($row['data']); $job->id = $row['id']; $job->header['timestamp'] = $row['timestamp']; return $job; }
/** * 批量插入订单的基本信息 * @param array $orderBaseArray * @throws \yii\db\Exception */ public function insertOrderBase(array $orderBaseArray) { if (!empty($orderBaseArray)) { $tableName = 't_oms_order'; $tableFields = []; $tableValues = []; foreach ($orderBaseArray as $orderBase) { if (empty($tableFields)) { $tableFields = array_keys($orderBase); } $tableValues[] = array_values($orderBase); } $transaction = $this->db->beginTransaction(); $this->db->createCommand()->batchInsert($tableName, $tableFields, $tableValues)->execute(); $transaction->commit(); } }
/** * Starts a transaction. * @param string|null $isolationLevel The isolation level to use for this transaction. * See [[Transaction::begin()]] for details. * @return Transaction the transaction initiated */ public function beginTransaction($isolationLevel = null) { $transaction = parent::beginTransaction(null); if ($isolationLevel !== null) { $transaction->setIsolationLevel($isolationLevel); } return $transaction; }
/** * Applies migration * * @param array $migration * @return void * @throws \yii\db\Exception */ protected function applyMigration($migration) { ob_start(); if ($this->direction == 'up') { $appliedMigrations = $this->getAppliedMigrations(); if (!isset($appliedMigrations[$migration['name']])) { $migrateObj = $this->createMigration($migration); if (method_exists($migrateObj, 'safeUp')) { $transaction = $this->_db->beginTransaction(); try { if ($migrateObj->safeUp() !== false) { $this->addMigrationHistory($migration); $transaction->commit(); } else { $transaction->rollBack(); } } catch (Exception $e) { $transaction->rollBack(); $this->error = $e; } } else { try { if ($migrateObj->up() !== false) { $this->addMigrationHistory($migration); } } catch (Exception $e) { $this->error = $e; } } } } elseif ($this->direction == 'down') { $migrateObj = $this->createMigration($migration); if (method_exists($migrateObj, 'safeDown')) { $transaction = $this->_db->beginTransaction(); try { if ($migrateObj->safeDown() !== false) { $this->removeMigrationHistory($migration); $transaction->commit(); } else { $transaction->rollBack(); } } catch (Exception $e) { $transaction->rollBack(); $this->error = $e; } } else { try { if ($migrateObj->down() !== false) { $this->removeMigrationHistory($migration); } } catch (Exception $e) { $this->error = $e; } } } ob_end_clean(); }
/** * Обновление базы данных */ public function import(array $translateArray) { $query = new Query(); $transaction = $this->_db->beginTransaction(); try { // Обходим каждую категорию foreach ($translateArray as $category => &$messages) { // обходим каждую константу foreach ($messages as $constant => &$message) { // Достаем константу с переводов в указанной категории $selectCategory = $query->from($this->_sourceMessageTable)->where(['category' => $category, 'message' => $constant])->createCommand($this->_db)->queryOne(); // Если такая константа уже существует, необходимо обновить перевод if ($selectCategory) { $constantId = $selectCategory['id']; } else { // Если такой константы нет, создадим ее $insert = $this->_db->createCommand()->insert($this->_sourceMessageTable, ['category' => $category, 'message' => $constant])->execute(); $constantId = $this->_db->lastInsertID; } // обходим каждый перевод сообщения foreach ($message as $lang => &$translate) { // Достаем перевод исходя из константы и языка $selectTranslate = $query->from($this->_messageTable)->where(['id' => $constantId, 'language' => $lang])->createCommand($this->_db)->queryOne(); // Если такой перевод есть, небходимо его обновить if ($selectTranslate) { // Обновляем только в том случае, если действительно есть изменения if ($selectTranslate['translation'] !== $translate && $this->_update) { $update = $this->_db->createCommand()->update($this->_messageTable, ['translation' => $translate], ['id' => $constantId, 'language' => $lang])->execute(); } } else { // Если перевода нет, вносим его $insert = $this->_db->createCommand()->insert($this->_messageTable, ['id' => $constantId, 'language' => $lang, 'translation' => $translate])->execute(); } } } } $transaction->commit(); return true; } catch (\Exception $e) { $transaction->rollBack(); throw new InvalidParamException($e->getMessage()); } }
/** * @param int|string $userId * @param string $key * @return bool * @throws \yii\db\Exception */ public function delete($userId, $key) { $transaction = $this->db->beginTransaction(); $result = $this->deleteFromDb($userId, $key) && $this->deleteFromCache($userId, $key); if ($result) { $transaction->commit(); } else { $transaction->rollBack(); } return $result; }
public function beginTransaction() { $this->db->beginTransaction(); }