Answer the question
In order to leave comments, you need to log in
Yii2 translation of dynamic content from the database - does not see the method from another model?
Good time everyone, please tell me what I'm doing wrong?
the article model does not see the method specified in article_lang, and data is not displayed in the index ???
to implement the translation of the dynamic content of the pages stored in the database,
I created 2 tables article and article_lang, created models and wrote links
for article
public function getArticles(){
return $this->find()->all();
}
public function getDataArticles(){
$language = Yii::$app->language;
$data_lang = $this->getArticleLangs()->where(['lang_url'=>$language])->one();
return $data_lang;
}
public function getArticle($id){
return $this->find()->where(['id' => $id])->one();
}
public function getArticleLangs()
{
return $this->hasOne(Article::class, ['id' => 'post_id']);
}
public function actionIndex(){
$article = new Article();
return $this->render('index', [
'articles' => $article->getArticles(),
]);
}
public function actionView($id)
{ $article = Article::findOne($id);
$lang_data = $article->getDataArticles();
return $this->render('single', [
'article' => $article,
'lang_data' => $lang_data,
]);
}
<?php $lang_data = $article->getDataArticles(); ?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question