Where condition

The standard syntax for action index is





public function actionIndex()

    {

        $searchModel = new BreakdownSearch();

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


        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }



In view you can choose the column to display and on the webpage you can filter on conditions,

But is it possible to pre-filter the data send to the view file, i.e instead of sending the full Model (Select * from table) to the view can we send only [size="2"](Select * from table Where condition) to the grid widget.[/size]

This works in initial view, but search doesn’t work!





public function actionIndex()

    {

        $searchModel = new BreakdownSearch();

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

	$dataProvider->query->where('breakdown.dept_id = 3');


        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }



The simplest and the most obvious solution works best





public function actionIndex()

    {

        $searchModel = new BreakdownSearch();

	$searchModel->dept_id = 3;

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


        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }



You could also modify $query inside your search model, like this:


$query = Breakdown::find()->andWhere(['dept_id' => 3])->all();

I think that’s a better approach.