M
M
Maxim Timofeev2017-03-17 13:19:33
Yii
Maxim Timofeev, 2017-03-17 13:19:33

How to compose search Model for MultilingualBehavior?

There is a behavior: https://github.com/OmgDef/yii2-multilingual-behavior
For internationalization. Completely turned off the logic for me. I can't make a serchModel. For the standard gridView, add the standard SearchModel.
Here is the behavior in the normal model:

public function behaviors()
    {
        return [
            'ml' => [
                'class' => MultilingualBehavior::className(),
                'languages' => [
                    'ru' => 'ru',
                    'en-US' => 'en',
                    'fr' => 'fr',
                ],
                'defaultLanguage' => 'en',
                'langForeignKey' => 'courses_cat_id',
                'tableName' => "{{%courses_cat_lang}}",
                'attributes' => [
                    'title', 'desc',
                ]
            ],
        ];
    }

I also added links there, such as through the behavior xs how to do:
public function getFr(){
        return $this->hasOne(CoursesCatLang::className(),['courses_cat_id'=>'id'])->andWhere(['language'=>'fr']);
    }

    public function getRu(){
        return $this->hasOne(CoursesCatLang::className(),['courses_cat_id'=>'id'])->andWhere(['language'=>'ru']);
    }

    public function getEn(){
        return $this->hasOne(CoursesCatLang::className(),['courses_cat_id'=>'id'])->andWhere(['language'=>'en']);
    }

Here is search as I saw it:
public function search($params)
    {
        $query = CoursesCat::find();
          $query->joinWith([
               'fr' => function ($q) {
               $q->from(['fr' => 'courses_cat_lang']);
               },
          ]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            return $dataProvider;
        }

        $query->andFilterWhere(['like', 'en.title', $this->title])
            ->andFilterWhere(['like', 'ru.title', $this->title_ru])
            ->andFilterWhere(['like', 'fr.title', $this->title_fr])
        return $dataProvider;
    }

With fr.title everything works fine, I can't figure out how to add other links.
tried like this:
$query->joinWith([
       'fr' => function ($q) {
           $q->from(['fr' => 'courses_cat_lang']);
       },
   ])->joinWith([
       'ru' => function ($q) {
           $q->from(['ru' => 'courses_cat_lang']);
       },
   ]);

I get:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'language' in where clause is ambiguous
The SQL being executed was: SELECT COUNT(*) FROM `courses_cat` LEFT JOIN `courses_cat_lang` `fr` ON `courses_cat`.`id` = `fr`.`courses_cat_id` LEFT JOIN `courses_cat_lang` `ru` ON `courses_cat`.`id` = `ru`.`courses_cat_id` WHERE (`language`='fr') AND (`language`='ru')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2017-03-17
@webinar

In general, I got out of the situation like this:
in the main model 1 connection:

public function getTrans(){
        return $this->hasOne(CoursesCatLang::className(),['courses_cat_id'=>'id']);
    }

in search model:
public function search($params)
    {
        $query = CoursesCat::find()->multilingual()->joinWith(['trans']);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            return $dataProvider;
        }

        if($this->title_en){
            $query->andFilterWhere(['courses_cat_lang.language'=>'en']);
        }elseif ($this->title_ru) {
            $query->andFilterWhere(['courses_cat_lang.language'=>'ru']);
        }elseif ($this->title_fr){
            $query->andFilterWhere(['courses_cat_lang.language'=>'fr']);
        }

        $query->andFilterWhere(['like', 'courses_cat_lang.title', $this->title_en])
            ->andFilterWhere(['like', 'courses_cat_lang.title', $this->title_ru])
            ->andFilterWhere(['like', 'courses_cat_lang.title', $this->title_fr]);
        return $dataProvider;
    }

S
Sabotage123, 2017-03-22
@Sabotage123

public function search($params)
{
$query = City::find()->active()->joinWith('translations');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
if($this->word){
$query->andFilterWhere([
'or',
['like', 'courses_cat_lang.title', $this->word],
]);
}
return $dataProvider;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question