2

I'm struggling to work out how to use a filter in my grid when it's generated from an SqlDataProvider.

Currently my model works like so:

 $sql = 'SELECT * FROM my_table';
 $provider = new SqlDataProvider([
       'sql' => $sql,
       'params' => [':start' => $param1, ':end' => $param2],
       'totalCount' => $count,
       'pagination' => [
         'pageSize' => 100
        ],
     ]);
return $provider;

When there isn't an active record how should this be done? The search models seem to all be based of AR and calling in the controller:

$searchModel->search(Yii::$app->request->queryParams);

Where $searchModel function begins like this:

  public function search($params)
 {
    $query = myTable::find();

    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);
    ...

I have started to change the search() function to be more like this, but not sure I'm going about it the right way:

$query = new Query;
    $query->select('*')
          ->from('my_table');

    // add conditions that should always apply here

    $dataProvider = new SqlDataProvider([
      'sql' => $query,
      'pagination' => [
        'pageSize' => 100
       ],
    ]);

As it errors with

preg_match() expects parameter 2 to be string, object given

2 Answers 2

2

For the SqlDataProvider you need to provide a SQL string. Here you are giving a Query Object, but you can get the SQL like this:

$query = new Query;
$query->select('*')
    ->from('my_table');

// add conditions that should always apply here

$dataProvider = new SqlDataProvider([
    'sql' => $query->createCommand()->sql,
    'pagination' => [
        'pageSize' => 100
    ],
]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I've just figured this out
1

For an activeRecord (generated by gii) realetd to a model you have a modelSearch.php tha containt a search function

In this case is a user model serach

 /**
 * @param $params
 * @return ActiveDataProvider
 */
public function search($params)
{
    $query = $this->finder->getUserQuery();

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    if ($this->created_at !== null) {
        $date = strtotime($this->created_at);
        $query->andFilterWhere(['between', 'created_at', $date, $date + 3600 * 24]);
    }

    $query->andFilterWhere(['like', 'username', $this->username])
        ->andFilterWhere(['like', 'email', $this->email])
        ->andFilterWhere(['registration_ip' => $this->registration_ip]);

    return $dataProvider;
}

the filter capabilities are performed with a proper combination of

 $query->andFilterWhere(....) 

each of this related to the proper column name and param var eg:

$query->andFilterWhere(['like', 'username', $this->username]);

this if that if you need a similar behavior for you model or you dataProvider you could repeat an similar pattern

$query = new Query;
$query->select('*')
      ->from('my_table');
$query->andFilterWhere('start', $param1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.