getDb() публичный статический Метод

By default, the "elasticsearch" application component is used as the database connection. You may override this method if you want to use a different database connection.
public static getDb ( ) : Connection
Результат Connection the database connection used by this AR class.
Пример #1
0
 /**
  * @param $documentClass \gromver\platform\common\models\elasticsearch\ActiveDocument
  * @return int
  * @throws \yii\elasticsearch\Exception
  */
 public function upload($documentClass)
 {
     $bulk = '';
     /** @var \yii\db\ActiveRecord|string $modelClass */
     $modelClass = $documentClass::model();
     /** @var \gromver\platform\common\models\elasticsearch\ActiveDocument $document */
     $document = new $documentClass();
     $uploaded = 0;
     foreach ($modelClass::find()->each() as $model) {
         /** @var \yii\db\ActiveRecord $model */
         $action = Json::encode(["index" => ["_id" => $model->getPrimaryKey(), "_type" => $documentClass::type(), "_index" => $documentClass::index()]]);
         $document->loadModel($model);
         $data = Json::encode($document->toArray());
         $bulk .= $action . "\n" . $data . "\n";
         $uploaded++;
     }
     $url = [$documentClass::index(), $documentClass::type(), '_bulk'];
     $response = ActiveRecord::getDb()->post($url, [], $bulk);
     $n = 0;
     $errors = [];
     foreach ($response['items'] as $item) {
         if (isset($item['index']['status']) && ($item['index']['status'] == 201 || $item['index']['status'] == 200)) {
             $n++;
         } else {
             $errors[] = $item['index'];
         }
     }
     if (!empty($errors) || isset($response['errors']) && $response['errors']) {
         throw new Exception(__METHOD__ . ' failed inserting ' . $modelClass . ' model records.', $errors);
     }
     return $n;
 }
 public function down()
 {
     $index = \gromver\platform\common\models\elasticsearch\ActiveDocument::index();
     \yii\elasticsearch\ActiveRecord::getDb()->createCommand()->deleteIndex($index);
     //->deleteAllIndexes();//
     echo "Index {$index} are deleted successfully.";
 }
 public function down()
 {
     if (!($index = Index::index())) {
         throw new Exception(Index::className() . '::index must be set.');
     }
     ActiveRecord::getDb()->createCommand()->deleteIndex($index);
     //->deleteAllIndexes();//
     echo "Index {$index} are deleted successfully.";
 }
 /**
  * @param $class \yii\db\ActiveRecord|string
  * @return int
  * @throws \yii\elasticsearch\Exception
  */
 public function upload($class)
 {
     $bulk = '';
     $index = Index::index();
     $type = Index::type();
     $timestamp = time();
     /** @var \yii\base\Behavior[] */
     $behaviors = (new $class())->behaviors;
     $query = $class::find();
     array_walk($behaviors, function ($behavior) use($query) {
         if ($behavior instanceof TaggableBehavior) {
             $query->with('tags');
         }
     });
     foreach ($query->each() as $model) {
         /** @var \yii\db\ActiveRecord|\gromver\platform\core\interfaces\model\SearchableInterface|\gromver\platform\core\interfaces\model\ViewableInterface $model */
         $action = Json::encode(["index" => ["_id" => $model->getPrimaryKey(), "_type" => $type, "_index" => $index]]);
         $indexModel = Index::findOne(['model_id' => $model->getPrimaryKey(), 'model_class' => $model->className()]) or $indexModel = new Index();
         $indexModel->model_id = $model->getPrimaryKey();
         $indexModel->model_class = $model->className();
         $indexModel->title = $model->getSearchTitle();
         $indexModel->content = $model->getSearchContent();
         $indexModel->tags = $model->getSearchTags();
         $indexModel->url_backend = $model->getBackendViewLink();
         $indexModel->url_frontend = $model->getFrontendViewLink();
         $indexModel->updated_at = $timestamp;
         ModuleEvent::trigger(Module::EVENT_BEFORE_CREATE_INDEX . $model->className(), new ElasticIndexEvent(['model' => $model, 'index' => $indexModel, 'sender' => $this->module]));
         if ($indexModel->validate()) {
             $bulk .= $action . "\n" . Json::encode($indexModel->toArray()) . "\n";
         }
     }
     $url = [$index, $type, '_bulk'];
     $response = ActiveRecord::getDb()->post($url, [], $bulk);
     $n = 0;
     $errors = [];
     foreach ($response['items'] as $item) {
         if (isset($item['index']['status']) && ($item['index']['status'] == 201 || $item['index']['status'] == 200)) {
             $n++;
         } else {
             $errors[] = $item['index'];
         }
     }
     if (!empty($errors) || isset($response['errors']) && $response['errors']) {
         throw new Exception(__METHOD__ . ' failed inserting ' . $class . ' model records.', $errors);
     }
     return $n;
 }