* @since 2.0 */ class ActiveRelation extends ActiveQuery implements ActiveRelationInterface { use ActiveRelationTrait; /** * Creates a DB command that can be used to execute this query. * @param Connection $db 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 ActiveRelation $viaQuery */ 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]); } } return parent::createCommand($db); } }