yii\elasticsearch\ActiveQuery::createCommand PHP Method

createCommand() public method

Creates a DB command that can be used to execute this query.
public createCommand ( Connection $db = null ) : Command
$db Connection the DB connection used to create the DB command. If null, the DB connection returned by [[modelClass]] will be used.
return Command the created DB command instance.
    public function createCommand($db = null)
    {
        if ($this->primaryModel !== null) {
            // lazy loading
            if (is_array($this->via)) {
                // via relation
                /* @var $viaQuery ActiveQuery */
                list($viaName, $viaQuery) = $this->via;
                if ($viaQuery->multiple) {
                    $viaModels = $viaQuery->all();
                    $this->primaryModel->populateRelation($viaName, $viaModels);
                } else {
                    $model = $viaQuery->one();
                    $this->primaryModel->populateRelation($viaName, $model);
                    $viaModels = $model === null ? [] : [$model];
                }
                $this->filterByModels($viaModels);
            } else {
                $this->filterByModels([$this->primaryModel]);
            }
        }
        /* @var $modelClass ActiveRecord */
        $modelClass = $this->modelClass;
        if ($db === null) {
            $db = $modelClass::getDb();
        }
        if ($this->type === null) {
            $this->type = $modelClass::type();
        }
        if ($this->index === null) {
            $this->index = $modelClass::index();
            $this->type = $modelClass::type();
        }
        $commandConfig = $db->getQueryBuilder()->build($this);
        return $db->createCommand($commandConfig);
    }

Usage Example

 /**
  * Creates a DB command that can be used to execute this query.
  * @param Connection $db the database connection used to execute the query.
  * If this parameter is not given, the `elasticsearch` application component will be used.
  * @return Command the created DB command instance.
  */
 public function createCommand($db = null)
 {
     $command = parent::createCommand($db);
     if ($this->indicesBoost !== null) {
         $command->queryParts['indices_boost'] = $this->indicesBoost;
     }
     return $command;
 }