Yii2搜索模型,不使用GridView [英] Yii2 Search model without using GridView

查看:116
本文介绍了Yii2搜索模型,不使用GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在房地产机构的主页上有一个serch表单。有关对象的数据存储在使用关系的realty表中。例如,有相关的表格类别(住宅,商业,地块),交易(买,卖,租),object_type(公寓,住宅,办公室)。

然后不同的类别具有不同的属性,并且在搜索表单中有三个引导选项卡:住宅,商业和地块。在每个选项卡下,选择和输入字段都是特定于选定选项卡的字段。

在大多数情况下,使用搜索模型的示例在gridview中给出。
是否可以修改Search模型逻辑,以便它可以根据主页上的搜索表单中指示的值返回表'realty'的结果数组?

解决方案

是的,当然可以。您有以下几种选择:

解决方案1 ​​ - 最糟糕的解决方案,但您的问题的答案

修改模型的搜索功能(或创建一个新功能)。搜索函数通常看起来像这样

 公共函数搜索($ params)
{
$ query = Bookmark :: find() - > where('status<>已删除'');
$ 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->提醒,
'order'=> $ this->订单,
'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]);
返回$ dataProvider;
}

您可以将其更改为

  public function search($ params)
{
$ query = Bookmark :: find() - > where('status<> ;已删除);
if(!($ this-> load($ params)&& $ this-> validate())){
在这里抛出错误
}

$ query-> andFilterWhere([
'status'=> $ this->状态,
'id'=> $ this-> id,
'reminder'=> $ this->提醒,
'order'=> $ this->命令,
'update_time'=> $ this-> update_time,
'update_by'=> $ this-> update_by,
'create_time'=> $ this-> create_time,
'create_by'=> Yii :: $ app->用户> id,
]);
$ query-> andFilterWhere(['like','name',$ this-> name])
- > andFilterWhere(['like','url',$ this-> ; URL]);
return $ query-> all();






但是这会返回给你所有的记录,因为ActiveDataProvider需要根据给出的查询处理分页。

解决方案2,更好的解决方案

在这里阅读第一个例子 http://www.yiiframework.com /doc-2.0/yii-data-activedataprovider.html 。您可以在ActiveDataProvider上调用 - > getModels()来获取实际记录。搜索功能无需更改。



解决方案3和我始终使用的内容



使用ActiveViewProvider和ListView。列表视图允许您根据需要创建记录列表,但不一定是表格。我个人在很多地方都这样做,而且效果很好。有时我会将数组转换为ArrayDataProvider以便使用它。有关数据提供者的更多信息,请访问: http://www.yiiframework.com /doc-2.0/guide-output-data-providers.html


There is a serch form on the mainpage of a realestate agency. The data about objects is stored in the table "realty" that uses relations. For example, there are related tables category (residential, commercial, land plots), deal (buy, sell, rent), object_type (apartment, house, office).

Then different categories have different properties and and there are three bootstrap tabs in the search form: residential, commercial, land plots. Under each tab there are selects and input fields that are specific for the choosen tab.

In the most cases, the examples of using Search model are given within a gridview. Is it possible to adapt Search model logic so that it could return the array of results from the table 'realty' based on the values indicated in the search form on the mainpage ?

解决方案

Yes, of course you can. you have several options:

Solution 1 - worst solution but the answer to your question

modify the search function of the model (or create a new function). The search functions usually looks 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 to you all the records because ActiveDataProvider takes care of the pagination based on the query given.

Solution 2, a better solution

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

Solution 3 and what I use all the time

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

这篇关于Yii2搜索模型,不使用GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆