Exemplo n.º 1
0
 /**
  * Executes query and returns a single row of result.
  * @param Connection $db the DB connection used to create the DB command.
  * If null, the DB connection returned by [[modelClass]] will be used.
  * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
  * the query result may be either an array or an ActiveRecord object. Null will be returned
  * if the query results in nothing.
  */
 public function one($db = null)
 {
     if (($result = parent::one($db)) === false) {
         return null;
     }
     if ($this->asArray) {
         return $result;
     } else {
         /* @var $class ActiveRecord */
         $class = $this->modelClass;
         $model = $class::instantiate($result);
         $class::populateRecord($model, $result);
         if (!empty($this->with)) {
             $models = [$model];
             $this->findWith($this->with, $models);
             $model = $models[0];
         }
         $model->afterFind();
         return $model;
     }
 }
 /**
  * @inheritdoc
  */
 protected function getMigrationHistory($limit)
 {
     $tables = [];
     foreach ($this->db->createCommand()->getTables() as $table) {
         $tables[] = $table['table_name'];
     }
     if (!in_array($this->migrationTable, $tables)) {
         $this->createMigrationHistoryTable();
     }
     $query = new Query();
     $rows = $query->select(['version', 'apply_time'])->from($this->migrationTable)->orderBy('version DESC')->limit($limit)->createCommand($this->db)->queryAll();
     $history = ArrayHelper::map($rows, 'version', 'apply_time');
     unset($history[self::BASE_MIGRATION]);
     return $history;
 }