本文整理汇总了PHP中CDbConnection::beginTransaction方法的典型用法代码示例。如果您正苦于以下问题:PHP CDbConnection::beginTransaction方法的具体用法?PHP CDbConnection::beginTransaction怎么用?PHP CDbConnection::beginTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CDbConnection
的用法示例。
在下文中一共展示了CDbConnection::beginTransaction方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testTransactions
public function testTransactions()
{
$transaction = $this->db->beginTransaction();
$schema = $this->db->schema;
$builder = $schema->commandBuilder;
$table = $schema->getTable('posts');
// Working transaction
try {
$builder->createInsertCommand($table, array('title' => 'working transaction test post 1', 'create_time' => '2009-01-01', 'author_id' => 1, 'content' => 'test content'))->execute();
$builder->createInsertCommand($table, array('title' => 'working transaction test post 2', 'create_time' => '2009-01-01', 'author_id' => 1, 'content' => 'test content'))->execute();
$transaction->commit();
} catch (Exception $e) {
$transaction->rollback();
}
$n = $builder->createCountCommand($table, new CDbCriteria(array('condition' => "title LIKE 'working transaction%'")))->queryScalar();
$this->assertEquals(2, $n);
// Failing Transaction
$transaction = $this->db->beginTransaction();
try {
$builder->createInsertCommand($table, array('title' => 'failed transaction test post 1', 'create_time' => '2009-01-01', 'author_id' => 1, 'content' => 'test content'))->execute();
$builder->createInsertCommand($table, array('id' => 1, 'title' => 'failed transaction test post 2', 'create_time' => '2009-01-01', 'author_id' => 1, 'content' => 'test content'))->execute();
$transaction->commit();
} catch (Exception $e) {
$transaction->rollback();
}
$n = $builder->createCountCommand($table, new CDbCriteria(array('condition' => "title LIKE 'failed transaction%'")))->queryScalar();
$this->assertEquals(0, $n);
}
示例2: beginTransaction
public function beginTransaction()
{
if (Yii::app()->params['enable_transactions']) {
return parent::beginTransaction();
} else {
$stub = new OETransactionStub();
return $stub;
}
}
示例3: runDbScript
private function runDbScript()
{
try {
$connection = new CDbConnection("{$this->dbDriver}:host={$this->dbHost};dbname={$this->dbName}", $this->dbUser, $this->dbPassword);
$connection->active = true;
$sqlFile = Yii::getPathOfAlias('webroot.protected.data') . '/schema.mysql.sql';
$sql = file_get_contents($sqlFile);
$connection->beginTransaction();
$connection->createCommand("SET CHARACTER SET utf8")->execute();
$command = $connection->createCommand($sql);
$command->execute(array(':username' => $this->username, ':salt' => $salt = substr(md5(uniqid(rand(), true)), 0, 9), ':password' => sha1($salt . sha1($salt . sha1($this->password))), ':email' => $this->email));
$connection->getCurrentTransaction()->commit();
$connection->active = false;
return true;
} catch (Exception $ex) {
return false;
}
}
示例4: beginTransaction
public function beginTransaction()
{
$this->db->beginTransaction();
}
示例5: _do_sync
private function _do_sync($localTable, $onlineTable, $isLocal)
{
$connection = null;
ini_set('max_execution_time', 9000);
if ($isLocal) {
$ipLocal = $_POST["ip"];
$connection = new CDbConnection("mysql:host={$ipLocal};dbname=sam;", "sam", "54MSyn#");
} else {
$connection = new CDbConnection("mysql:host=sam.cbn26zgvuhet.us-east-1.rds.amazonaws.com;dbname=sam;", "mggroup", "5am2oo14#");
}
$connection->active = true;
$transaction = $connection->beginTransaction();
try {
$ifMatricula = "";
if ($localTable == "aud_matricula") {
$ifMatricula = " estado_auditoria_id IS NOT NULL\n AND ";
} else {
$ifMatricula = "";
}
$queryInsert = "";
if ($isLocal) {
$queryInsert = "SELECT\n *\n FROM\n{$localTable}\n WHERE fecha_actualizacion IS NOT NULL";
} else {
$queryInsert = "SELECT\n *\n FROM\n{$localTable}\n WHERE fecha_sincronizacion IS NULL\n AND fecha_actualizacion IS NOT NULL\n OR fecha_sincronizacion < fecha_actualizacion limit 1000";
}
$dataLocal = Yii::app()->db->createCommand($queryInsert)->queryAll();
$command = $connection->createCommand();
foreach ($dataLocal as $item => $itemValue) {
if (!$isLocal) {
$itemValue["fecha_sincronizacion"] = date("Y-m-d H:i:s");
}
$command->insert($onlineTable, $itemValue);
}
$transaction->commit();
if (!$isLocal) {
$queryInsert = "UPDATE\n{$localTableSET}\n" . $localTable . " .fecha_sincronizacion = NOW()\n WHERE fecha_sincronizacion IS NULL\n AND fecha_actualizacion IS NOT NULL\n OR fecha_sincronizacion < fecha_actualizacion ";
Yii::app()->db->createCommand($queryInsert)->execute();
}
return true;
} catch (Exception $exc) {
$transaction->rollBack();
echo $exc->getMessage();
return false;
}
}