/** * Finds an instance of the owner based on the provided alias and language * * @param string $alias The alias * @param mixed $language The language * @return ActiveQuery */ public static function findByAlias($alias = '', $language = null) { // Try to load an Alias with provided params $aliasModel = Alias::findOne(['url' => $alias, 'language' => $language ?: Yii::$app->language, 'entity' => parent::className()]); if (!$aliasModel) { throw new \yii\web\NotFoundHttpException(); } return parent::find()->where(['id' => $aliasModel->entity_id]); }
/** * Returns the Alias model. * If it does not exist the first time this method is called, it is loaded * or created and cached. * * @return Alias */ public function getAlias() { // Load Alias if (!$this->aliasModel) { $conditions = ['entity' => $this->entityType, 'entity_id' => $this->owner->{$this->entityIdField}, 'language' => $this->owner->{$this->languageField}]; $this->aliasModel = Alias::findOne($conditions); // Create Alias if (!$this->aliasModel) { $this->aliasModel = new Alias($conditions); } } return $this->aliasModel; }
/** * Finds the Alias model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return Alias the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Alias::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist')); } }