Hello, i’m making, a globalSearch for my Client’s model. But i’ve made another table “Telephone” that contains all the phone numbers for my Client. So i created a hasMany relation between them in Client’s Model.
/**
* @return \yii\db\ActiveQuery
*/
public function getTelephones()
{
return $this->hasMany(Telephone::className(), ['client_id' => 'client_id']);
}
Now i need to search for my Clients in Client’s index with GridView by these telephone numbers from table ‘Telephone’. I’ve searched in google for 2 days and cannot find the solution with using ->orFilterWhere function. Can someone please help me do that here is my ClientSearch model.
<?php namespace app\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Client;
/**
* ClientSearch represents the model behind the search form of `app\models\Client`.
*/
class ClientSearch extends Client
{
public $globalSearch;
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['globalSearch'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Client::find();
// will eagerly load the related models
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> [
'attributes' => ['first_name', 'last_name', 'email'],
'defaultOrder' => ['first_name'=>SORT_ASC, 'last_name'=>SORT_ASC]
]
]);
$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;
}
// grid filtering conditions
$query->andFilterWhere([
'client_id' => $this->client_id,
]);
$query->orFilterWhere(['like', 'Concat(first_name," ", last_name)', $this->globalSearch])
->orFilterWhere(['like', 'first_name', $this->globalSearch])
->orFilterWhere(['like', 'last_name', $this->globalSearch])
->orFilterWhere(['like', 'email', $this->globalSearch])
->orFilterWhere(['like', 'address', $this->globalSearch])
->orFilterWhere(['like', 'client_id', $this->globalSearch]);
return $dataProvider;
}
}
Please help me when i search for any of the numbers related to the Client to show me the Client in the GridView.
Telephone model have - ID, client_id and telephone as attributes.