Yii2 Model Search Without GridView

There is a form on the home page of a real estate agency. Object data is stored in the "realty" table, which uses relationships. For example, there are categories of related categories (residential, commercial, land), transactions (purchase, sale, rental), object_type (apartment, house, office).

Then different categories have different properties, and in the search form there are three boot tabs: residential, commercial, land. Under each tab there are selection and input fields that are specific to the selected tab.

In most cases, examples of using the search model are shown as a grid. Is it possible to adapt the logic of the search model so that it can return an array of results from the "realty" table based on the values ​​specified in the search form on the main page?

+1
search gridview yii2
source share
1 answer

Yes of course you can. You have several options:

Solution 1 is the worst solution, but the answer to your question

change the model search function (or create a new function). Search functions usually look like this:

public function search($params) { $query = Bookmark::find()->where('status <> "deleted"'); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => Yii::$app->session->get(get_parent_class($this) . 'Pagination'), ], ]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere([ 'status' => $this->status, 'id' => $this->id, 'reminder' => $this->reminder, 'order' => $this->order, 'update_time' => $this->update_time, 'update_by' => $this->update_by, 'create_time' => $this->create_time, 'create_by' => Yii::$app->user->id, ]); $query->andFilterWhere(['like', 'name', $this->name]) ->andFilterWhere(['like', 'url', $this->url]); return $dataProvider; } 

You can change it to something like

 public function search($params) { $query = Bookmark::find()->where('status <> "deleted"'); if (!($this->load($params) && $this->validate())) { THROW AN ERROR SOMEHOW HERE } $query->andFilterWhere([ 'status' => $this->status, 'id' => $this->id, 'reminder' => $this->reminder, 'order' => $this->order, 'update_time' => $this->update_time, 'update_by' => $this->update_by, 'create_time' => $this->create_time, 'create_by' => Yii::$app->user->id, ]); $query->andFilterWhere(['like', 'name', $this->name]) ->andFilterWhere(['like', 'url', $this->url]); return $query->all(); } 

however, this will return all the records to you, because ActiveDataProvider will take care of pagination based on the given request.

Solution 2, the best solution

read the first example here http://www.yiiframework.com/doc-2.0/yii-data-activedataprovider.html . You can call ->getModels() on ActiveDataProvider to get the actual records. No changes required for the search function. Do whatever you want with an array.

Solution 3 and all that I use all the time

Use ActiveDataProvider with ListView. The list view allows you to create a list of records, but you do not want it to be a table. I personally do this in many places, and it works very well. I sometimes convert arrays to ArrayDataProvider to use it. More information about data providers here: http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html

+4
source share

All Articles