Beispiel #1
0
 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);
 }
Beispiel #2
0
 public function beginTransaction()
 {
     if (Yii::app()->params['enable_transactions']) {
         return parent::beginTransaction();
     } else {
         $stub = new OETransactionStub();
         return $stub;
     }
 }
Beispiel #3
0
 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;
     }
 }
 public function beginTransaction()
 {
     $this->db->beginTransaction();
 }
 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;
     }
 }