本文整理汇总了PHP中Illuminate\Database\Connection::transaction方法的典型用法代码示例。如果您正苦于以下问题:PHP Connection::transaction方法的具体用法?PHP Connection::transaction怎么用?PHP Connection::transaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Connection
的用法示例。
在下文中一共展示了Connection::transaction方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: ensureTransaction
/**
* Enures the given closur is executed within a PDO transaction.
*
* @param Closure $callback
* @return void
*/
public function ensureTransaction(Closure $callback)
{
if (!$this->connection->getPdo()->inTransaction()) {
$this->connection->transaction($callback);
} else {
$callback($this->connection);
}
}
示例2: requestPasswordResetForEmail
/**
* Request a password reset for a given email.
*
* @param string $email
*
* @return bool
*/
public function requestPasswordResetForEmail($email)
{
if ($this->reminders === null) {
throw new \RuntimeException('Password reset service not set.');
}
return $this->db->transaction(function () use($email) {
$user = $this->users->findByCredentials(['email' => $email]);
if (!$user) {
throw new Reminders\ReminderException();
}
return $this->reminders->requestReset($user);
});
}
示例3: transaction
/**
* Execute a Closure within a transaction.
*
* @param \Closure $callback
* @return mixed
*
* @throws \Exception
*/
public function transaction(Closure $callback)
{
if ($this->getDriverName() == 'sqlsrv') {
return parent::transaction($callback);
}
$this->pdo->exec('BEGIN TRAN');
// We'll simply execute the given callback within a try / catch block
// and if we catch any exception we can rollback the transaction
// so that none of the changes are persisted to the database.
try {
$result = $callback($this);
$this->pdo->exec('COMMIT TRAN');
} catch (Exception $e) {
$this->pdo->exec('ROLLBACK TRAN');
throw $e;
}
return $result;
}
示例4: revokePrivileges
/**
* @param Connection $db
* @param array $creds
* @param string $fromServer
*
* @return bool
*/
protected function revokePrivileges($db, $creds, $fromServer)
{
return $db->transaction(function () use($db, $creds, $fromServer) {
// Create users
$_users = $this->getDatabaseUsers($creds, $fromServer);
try {
foreach ($_users as $_user) {
// Grants for instance database
if (!($_result = $db->statement('REVOKE ALL PRIVILEGES ON ' . $creds['database'] . '.* FROM ' . $_user))) {
$this->error('[provisioning:database] error revoking privileges from "' . $_user . '"');
continue;
}
$this->debug('[provisioning:database] grants revoked - complete');
if (!($_result = $db->statement('DROP USER ' . $_user))) {
$this->error('[provisioning:database] error dropping user "' . $_user . '"');
}
$_result && $this->debug('[provisioning:database] users dropped > ', $_users);
}
return true;
} catch (\Exception $_ex) {
$this->error('[provisioning:database] revoke grants - failure: ' . $_ex->getMessage());
return false;
}
});
}