本文整理汇总了PHP中yii\db\Connection::beginTransaction方法的典型用法代码示例。如果您正苦于以下问题:PHP Connection::beginTransaction方法的具体用法?PHP Connection::beginTransaction怎么用?PHP Connection::beginTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\Connection
的用法示例。
在下文中一共展示了Connection::beginTransaction方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fetchJob
/**
* 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;
}
示例2: insertOrderBase
/**
* 批量插入订单的基本信息
* @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();
}
}
示例3: beginTransaction
/**
* 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;
}
示例4: applyMigration
/**
* 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();
}
示例5: import
/**
* Обновление базы данных
*/
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());
}
}
示例6: delete
/**
* @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;
}
示例7: beginTransaction
public function beginTransaction()
{
$this->db->beginTransaction();
}
示例8: actionUpdate
/**
* @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();
}
}