Ejemplo n.º 1
0
 /**
  * Saves a connection
  * @param string $name Name of the connection
  * @param string $dsn DSN string for the connection
  * @param string $oldName Old name if we are doing a rename action
  * @return null
  * @throws Exception when an error occurs
  */
 public function saveConnection($name, $dsn, $oldName = null)
 {
     $zibo = Zibo::getInstance();
     $updateDefault = false;
     $baseKey = DatabaseManager::CONFIG_CONNECTION . Config::TOKEN_SEPARATOR;
     if (!empty($oldName) && $name != $oldName) {
         $defaultConnection = $this->getDefaultConnection();
         if ($defaultConnection->getName() == $oldName) {
             $updateDefault = true;
         }
         $zibo->setConfigValue($baseKey . $oldName, null);
     }
     $dsn = new Dsn($dsn);
     try {
         $this->manager->registerConnection($name, $dsn);
         $zibo->setConfigValue($baseKey . $name, $dsn->__toString());
         if ($updateDefault) {
             $this->setDefaultConnection($name);
         }
     } catch (Exception $exception) {
         $error = new ValidationError(self::TRANSLATION_ERROR, '%error%', array('error' => $exception->getMessage()));
         $validationException = new ValidationException();
         $validationException->addErrors('dsn', array($error));
         throw $validationException;
     }
 }
Ejemplo n.º 2
0
 public function tearDown()
 {
     $connection = DatabaseManager::getInstance()->getConnection();
     $connection->executeFile(new File(self::SQL_TEARDOWN));
     //        $definer = new ModelDefiner();
     //        $definer->deleteModels($models);
     parent::tearDown();
 }
Ejemplo n.º 3
0
 /**
  * Creates or alters the tables in the database of the provided models
  * @param array $models Array with Model objects
  * @return null
  */
 public function defineModels(array $models)
 {
     $connection = DatabaseManager::getInstance()->getConnection();
     $definer = $connection->getDefiner();
     $isTransactionStarted = $connection->startTransaction();
     try {
         $tables = array();
         foreach ($models as $model) {
             $table = $this->getDatabaseTable($model);
             $definer->defineTable($table);
             $tables[] = $table;
         }
         foreach ($tables as $table) {
             $definer->defineForeignKeys($table);
         }
         $connection->commitTransaction($isTransactionStarted);
     } catch (Exception $exception) {
         $connection->rollbackTransaction($isTransactionStarted);
         throw $exception;
     }
 }
Ejemplo n.º 4
0
 /**
  * Constructs a new database connection model
  * @return null
  */
 public function __construct()
 {
     $this->manager = DatabaseManager::getInstance();
 }
Ejemplo n.º 5
0
 /**
  * Deletes the models for the uninstalled modules
  * @param zibo\admin\model\Installer $installer Instance of the installer
  * @param zibo\library\filesystem\File $modulePath Path of the modules
  * @param array $modules The modules which are uninstalled
  * @return null
  */
 public function deleteModelsForUninstalledModules(Installer $installer, File $modulePath, array $modules)
 {
     $connections = DatabaseManager::getInstance()->getConnections();
     if (!$connections) {
         return;
     }
     ModelManager::getInstance()->defineModels();
 }
 /**
  * @expectedException zibo\library\database\exception\DatabaseException
  */
 public function testSetDefaultConnectionThrowsExceptionWhenNameDoesNotExist()
 {
     $manager = DatabaseManager::getInstance();
     $manager->setDefaultConnectionName('unexistant');
 }
Ejemplo n.º 7
0
 /**
  * Gets all the field types as defined in the database definer
  * @return array Array with the property type as key and as value
  */
 private function getPropertyTypes()
 {
     $connection = DatabaseManager::getInstance()->getConnection();
     $definer = $connection->getDefiner();
     $fieldTypes = $definer->getFieldTypes();
     $propertyTypes = array();
     foreach ($fieldTypes as $fieldType => $databaseType) {
         $propertyTypes[$fieldType] = $fieldType;
     }
     return $propertyTypes;
 }
Ejemplo n.º 8
0
 protected function tearDown()
 {
     Reflection::setProperty(ModelManager::getInstance(), 'instance', null);
     Reflection::setProperty(DatabaseManager::getInstance(), 'instance', null);
     Reflection::setProperty(Zibo::getInstance(), 'instance', null);
 }
Ejemplo n.º 9
0
 /**
  * Gets the driver of the database connection
  * @return zibo\library\database\driver\Driver
  */
 public function getConnection()
 {
     return DatabaseManager::getInstance()->getConnection();
 }
Ejemplo n.º 10
0
 /**
  * @expectedException zibo\library\database\exception\DatabaseException
  */
 public function testSetDefaultConnectionThrowsExceptionWhenNameDoesNotExist()
 {
     $manager = new DatabaseManager($this->zibo);
     $manager->setDefaultConnectionName('unexistant');
 }