Example #1
0
 public function updateColumns(Base $record, array $columnsValuesPairs)
 {
     if ($record->isNewRecord()) {
         throw new Exception\RuntimeException("Can't update columns on a new record");
     }
     return $this->updateRecord($record, $columnsValuesPairs);
 }
Example #2
0
 protected function task()
 {
     $adapter = SqlBase::adapter();
     $filesDir = $this->app->config()['paths']['root']->expand('db');
     $migrator = new Migrator($adapter, $filesDir);
     $migrator->run();
 }
Example #3
0
 protected function setUpDatabase(Application $application)
 {
     $connManager = $application->getService('defaultConnectionManager');
     $connName = 'test';
     if ($connManager->connectionExists($connName)) {
         $connManager->setDefaultConnection($connName);
         $adapter = $connManager->getAdapter($connName);
         if ($adapter instanceof MongoConnection) {
             $adapter->database()->drop();
         } else {
             $params = $adapter->getDriver()->getConnection()->getConnectionParameters();
             $filesDir = $application->config()['paths']['root']->expand('db');
             $schema = new Schema($adapter);
             $schema->dropDatabase($params['database']);
             $schema->createDatabase($params['database']);
             # Reset connection.
             $connManager->removeAdapter($connName);
             $adapter = $connManager->getAdapter($connName);
             $migrator = new Migrator($adapter, $filesDir);
             $migrator->run();
             # Clear table data.
             ActiveRecordBase::clearMetadatas();
             ActiveRecordBase::clearModelSchemas();
             SchemaMigration::setAdapter($adapter);
         }
     }
 }
Example #4
0
 protected function task()
 {
     $file = $this->app->config()['paths']['root']->expand('db', 'schema.sql');
     $connection = SqlBase::adapter()->getDriver()->getConnection();
     $exporter = new Exporter();
     $dump = $exporter->export($connection);
     file_put_contents($file, $dump);
 }
 protected function task()
 {
     $sqlFile = $this->app->config()['paths']['root']->expand('db', 'schema.sql');
     if (!is_file($sqlFile)) {
         $this->output->write("File db/schema.sql not found");
         return;
     }
     $connection = SqlBase::adapter()->getDriver()->getConnection();
     $importer = new Importer();
     $importer->import($connection, $sqlFile);
 }
Example #6
0
 public function set(Base $record, $name, $value)
 {
     if ($value !== null && !$value instanceof Base) {
         if (is_object($value)) {
             $message = sprintf("Must pass instance of Rails\\ActiveRecord\\Base as value, instance of %s passed", get_class($value));
         } else {
             $message = sprintf("Must pass either null or instance of Rails\\ActiveRecord\\Base as value, %s passed", gettype($value));
         }
         throw new Exception\InvalidArgumentException($message);
     }
     $options = $record->getAssociations()->get($name);
     switch ($options['type']) {
         case 'belongsTo':
             if ($value) {
                 $this->matchClass($value, $options['className']);
                 $value = $value->id();
             }
             $record->setAttribute($options['foreignKey'], $value);
             break;
         case 'hasOne':
             $foreignKey = $options['foreignKey'];
             if ($value) {
                 $this->matchClass($value, $options['className']);
                 $value->setAttribute($foreignKey, $record->id());
             }
             if ($record->isNewRecord()) {
                 return;
             }
             $oldValue = $record->getAssociation($name);
             if ($value && $oldValue && $value->getAttribute($foreignKey) == $oldValue->getAttribute($foreignKey)) {
                 return;
             }
             if ($oldValue) {
                 $oldValue->setAttribute($foreignKey, null);
             }
             if (!static::transaction(function () use($name, $value, $oldValue) {
                 if ($value) {
                     if (!$value->save()) {
                         return false;
                     }
                 }
                 if ($oldValue) {
                     if (!$oldValue->save()) {
                         return false;
                     }
                 }
             })) {
                 throw new RecordNotSavedException(sprinf("Failed to save new associated %s", strtolower($record::getService('inflector')->underscore($name)->humanize())));
             }
             break;
     }
     return true;
 }