yii2-multilingual-behavior
yii2-multilingual-behavior copied to clipboard
How to change CRUD generated Search model to search by multilingual fields
This code:
public function search($params)
{
$query = Object::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'raionId' => $this->raionId,
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'elevation' => $this->elevation,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'shortDescription', $this->shortDescription])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['like', 'phone', $this->phone])
->andFilterWhere(['like', 'email', $this->email])
->andFilterWhere(['like', 'site', $this->site])
->andFilterWhere(['like', 'skype', $this->skype])
->andFilterWhere(['like', 'youTube', $this->youTube])
->andFilterWhere(['like', 'facebook', $this->facebook])
->andFilterWhere(['like', 'googleplus', $this->googleplus])
->andFilterWhere(['like', 'vk', $this->vk])
->andFilterWhere(['like', 'twitter', $this->twitter])
->andFilterWhere(['like', 'tripadvisor', $this->tripadvisor])
->andFilterWhere(['like', 'instagram', $this->instagram])
->andFilterWhere(['like', 'foursquare', $this->foursquare])
->andFilterWhere(['like', 'address', $this->address])
->andFilterWhere(['like', 'postalCode', $this->postalCode]);
return $dataProvider;
}
throws SQL error when params include multilingual fields.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause'
The SQL being executed was: SELECT COUNT(*) FROM `object` WHERE `name` LIKE '%Ho%'
@euglv
Example
public function search($params)
{
$query = Order::find()->andWhere([static::tableName() . '.locked' => 0])->joinWith('user')->orderBy('id desc');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
static::tableName() . '.id' => $this->id,
static::tableName() . '.status' => $this->status,
static::tableName() . '.type' => $this->type,
static::tableName() . '.payment_type' => $this->payment_type,
static::tableName() . '.restaurant_id' => $this->restaurant_id,
static::tableName() . '.delivery_time' => $this->delivery_time,
static::tableName() . '.discount_percent' => $this->discount_percent,
static::tableName() . '.new' => $this->new,
static::tableName() . '.first' => $this->first,
static::tableName() . '.locked' => $this->locked,
static::tableName() . '.persons' => $this->persons,
static::tableName() . '.price' => $this->price,
static::tableName() . '.sale_price' => $this->sale_price,
static::tableName() . '.user_id' => $this->user_id,
static::tableName() . '.created_at' => $this->created_at,
static::tableName() . '.updated_at' => $this->updated_at,
]);
if ($this->phone) {
$this->phone = AppHelper::clearPhone($this->phone);
}
$query
->andFilterWhere(['like', static::tableName() . '.clear_phone', $this->phone])
->orFilterWhere(['like', User::tableName() . '.phone_clean', $this->phone])
->andFilterWhere(['like', static::tableName() . '.name', $this->name])
->orFilterWhere(['like', User::tableName() . '.username', $this->name])
->andFilterWhere(['like', static::tableName() . '.comment', $this->comment])
->andFilterWhere(['like', static::tableName() . '.sale_text', $this->sale_text]);
return $dataProvider;
}
In your case
public function search($params)
{
$query = Object::find()->joinWith('translations');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
//And so on
}
I'm use like this script but problem with summary of gridview. I have 10 record but in summary of gridview show 20 items
@kongoon call distinct()
in your query. Object::find()->joinWith('translations')->distinct()