H
H
Helen Izotova2019-02-18 16:51:35
Yii
Helen Izotova, 2019-02-18 16:51:35

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();
    }

for article_lang model
public function getArticleLangs()
    {
        return $this->hasOne(Article::class, ['id' => 'post_id']);
    }

added to controller
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,
]);
    }

when outputting to index, it
<?php $lang_data = $article->getDataArticles(); ?>
swears at
Unknown Method - yii\base\UnknownMethodException
Calling unknown method: app\models\Article::getArticleLangs()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question