Closed ustmaestro closed 9 years ago
@ustmaestro You should use joinWith
Post::find()->joinWith('translation')->andWhere(['{{%activities_lang}}. slug' => $slug])->one()
This is a simple example how to use search by a translated field
Supposed you have links like:
http://127.0.0.0/en/blog/post/demo-article http://127.0.0.0/ru/blog/post/demo-statiya http://127.0.0.0/ro/blog/post/demo-articol
where
demo-article
demo-statiya
demo-articol
are the slug's of same post in different languages en, ru, ro
In our url rules we need to pass this rule
'blog/post/<slug:[\w-]+>' => 'blog/post/slug', // if blog module
'blog/post/<slug:[\w-]+>' => 'post/slug', // if blog is not a module
to find post by slug in our PostController we need an action called "actionSlug" and a function that will search model by slug
/**
* Displays a single Post model.
* @param string $slug
* @return mixed
*/
public function actionSlug($slug)
{
$model = $this->findModelBySlug($slug);
$this->counter($model);
return $this->render('view', [ 'model' => $model ]);
}
And search by slug field in translation table
/**
* Finds the Post model based on slug.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $slug
* @return Post the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModelBySlug($slug)
{
if (( $model = Post::find()
->joinWith('translation') // join translation table
->andWhere([
PostLang::tableName().'.slug' => $slug, // add slug condition
PostLang::tableName().'.language' => Yii::$app->language, // add language condition
'status' => POST::STATUS_ACTIVE // add status condition
])
->one()
) !== null) {
return $model;
} else {
throw new NotFoundHttpException(Yii::t('blog','The requested post does not exist.'));
}
return null;
}
Hope this will be useful.
@OmgDef You have a typo {{%activities_lang} must be {{%post_lang}
@ustmaestro It seems you solved your problem. Can I close the issue?
OK. Can you please check https://github.com/OmgDef/yii2-multilingual-behavior/issues/23
First of all this is a very useful extension, I worked with it in Yii 1.x version and I want to say that is just awesome. Now I'm working with Yii 2.x version and i have a little problem
How to search in translated fields? Examples please!
Migrations:
Controller
I don't know how to make this work. In previous version I used Criteria.
Can you please create an example with search in translated fields