for my requirement I need to use SqlDataProvider instead of ActiveDataProvider.
but if I enable search I am getting the error like:
Calling unknown method: yii\data\SqlDataProvider::isAttributeRequired()
my searchModel - `userProfileSearch looks like this:
public function search($params)
{
// $query = UserProfile::find()->where($cond);
$query = new Query;
$query->select('*')->from('user_profile');
// add conditions that should always apply here
$dataProvider = new SqlDataProvider([
// 'query' => $query,
'sql' => $query->createCommand()->sql,
]);
$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([
'id' => $this['id'],
'user_id' => $this['user_id'],
'pincode' => $this['pincode'],
]);
$query->andFilterWhere(['like', 'first_name', $this['first_name']])
->andFilterWhere(['like', 'last_name', $this->last_name])
->andFilterWhere(['like', 'phone', $this->phone])
->andFilterWhere(['like', 'email', $this->email])
->andFilterWhere(['like', 'gender', $this->gender])
->andFilterWhere(['like', 'skill_level', $this->skill_level])
->andFilterWhere(['like', 'play_type', $this->play_type])
->andFilterWhere(['like', 'address1', $this->address1])
->andFilterWhere(['like', 'address2', $this->address2])
->andFilterWhere(['like', 'city', $this->city])
->andFilterWhere(['like', 'state', $this->state])
->andFilterWhere(['like', 'Country', $this->country]);
return $dataProvider;
}
and in my controller it looks like this:
public function actionIndex()
{
$count = Yii::$app->db->createCommand('SELECT COUNT(*) FROM user_profile')->queryScalar();
$sql= "$sql= "Select user_profile.id as id,
user_profile.user_id as user_id,
user_profile.first_name as first_name,
user_profile.last_name as last_name,
user_profile.city as city,
user_profile.pincode as pincode,
user_profile.profile_image as profile_image,
user_profile.gender as gender,
user_profile.play_type as play_type,
user_profile.skill_level as skill_level,
( 3959 * acos
(cos ( radians($latitude))
* cos( radians( latitude ))
* cos( radians( longitude) - radians($longitude))
+ sin ( radians($latitude) )
* sin( radians( latitude))))
AS distance
FROM user_profile
HAVING distance < 20";";
$searchModel = new UserProfileSearch();
$sqlProvider = new SqlDataProvider(['sql'=> $sql]);
$searchModel = $searchModel->search(Yii::$app->request->queryParams,$sql);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $sqlProvider,
]);
}
How I can make the search work?
<?php $form->field($model, 'gender')->dropDownList(['M'=>'Male','F'=>'Female'],['prompt'=>'Select Gender'])->label(false) ?>will it not work or where I need to make any modificaiton?