Answer the question
In order to leave comments, you need to log in
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',
]
],
];
}
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']);
}
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;
}
$query->joinWith([
'fr' => function ($q) {
$q->from(['fr' => 'courses_cat_lang']);
},
])->joinWith([
'ru' => function ($q) {
$q->from(['ru' => 'courses_cat_lang']);
},
]);
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
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']);
}
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;
}
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 questionAsk a Question
731 491 924 answers to any question