yii\db\ActiveQuery::alias PHP Method

alias() public method

This method will adjust [[from]] so that an already defined alias will be overwritten. If none was defined, [[from]] will be populated with the given alias.
Since: 2.0.7
public alias ( string $alias )
$alias string the table alias.
    public function alias($alias)
    {
        if (empty($this->from) || count($this->from) < 2) {
            list($tableName, ) = $this->getQueryTableName($this);
            $this->from = [$alias => $tableName];
        } else {
            /* @var $modelClass ActiveRecord */
            $modelClass = $this->modelClass;
            $tableName = $modelClass::tableName();
            foreach ($this->from as $key => $table) {
                if ($table === $tableName) {
                    unset($this->from[$key]);
                    $this->from[$alias] = $tableName;
                }
            }
        }
        return $this;
    }

Usage Example

Example #1
0
 /**
  * @inheritdoc
  */
 public function alias($alias)
 {
     $this->alias = null;
     $result = parent::alias($alias);
     $this->trigger(static::EVENT_ALIAS);
     return $result;
 }