【问题标题】:How to add Search and Filter Criteria in Yii如何在 Yii 中添加搜索和过滤条件
【发布时间】:2015-04-20 21:46:46
【问题描述】:

我正在使用 Yii 框架开发一个数据库应用程序。我正在从 MySQL 数据库中读取表并将它们显示给用户。我需要用户能够过滤表中的字段或搜索某个值。

例如,我有一个名为“超市”的表:

CREATE TABLE IF NOT EXISTS `supermarkets` (
  `Name` varchar(71) NOT NULL,
  `Location` varchar(191) DEFAULT NULL,
  `Telephone` varchar(68) DEFAULT NULL,
  `Fax` varchar(29) DEFAULT NULL,
  `Website` varchar(24) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

.../模特/超市:

<?php

namespace app\models;

use yii\db\ActiveRecord;

class Supermarkets extends ActiveRecord
{



}

.../views/supermarkets/index.php:

<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Supermarkets</h1>
<ul>
<?php
$array = (array) $supermarkets;

function build_table($array){

    // start table

    $html = '<table class="altrowstable" id="alternatecolor">';

    // header row

    $html .= '<tr>';

    foreach($array[0] as $key=>$value){

            $html .= '<th>' . $key . '</th>';

        }

    $html .= '</tr>';

    // data rows

    foreach( $array as $key=>$value){

        $html .= '<tr>';

        foreach($value as $key2=>$value2){

            $html .= '<td>' . $value2 . '</td>';

        }

        $html .= '</tr>';

    }

    // finish table and return it

    $html .= '</table>';

    return $html;

}



echo build_table($array);

?>

....控制器/超市控制器:

<?php

namespace app\controllers;

use yii\web\Controller;

use yii\data\Pagination;
use app\models\Supermarkets;
class SupermarketsController extends Controller
{
    public function actionIndex()
    {
        $query = supermarkets::find();

        $pagination = new Pagination([
            'defaultPageSize' => 20,
            'totalCount' => $query->count(),
        ]);

        $supermarkets = $query->orderBy('Name')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

        return $this->render('index', [
            'supermarkets' => $supermarkets,
            'pagination' => $pagination,
        ]);
    }
}

我需要用户能够过滤表格或按一个或多个属性搜索其字段。我该怎么做?

【问题讨论】:

  • 关于您的数据库表有两点值得一提的是: 1. 不要将列名大写。 2. 使用“id”字段来唯一标识超市。这可以是一个自动增量列。
  • 你说得对,谢谢。

标签: php mysql search yii filter


【解决方案1】:

您可以简单地使用 Yii 提供的 CGridView widget,而不是试图重新发明轮子。它具有排序和过滤功能。查看文档,您会发现有很多配置可供您使用。以下代码 sn -p 使用最低配置。

.../views/supermarkets/index.php:

    <?php
    $this->widget('zii.widgets.grid.CGridView', array(
        'id' => 'supermarkets-grid',
        'dataProvider' => $model->search(),
        'filter' => $model,
        'columns' => array(
            'name',
            'location',
            'telephone',
            'fax',
            'website'
        ),
    ));
    ?>

在 Supermarkets 模型中实现 search() 函数。

public function search()
{

    $criteria=new CDbCriteria;

    $criteria->compare('name',$this->name,true);
    $criteria->compare('location',$this->location,true);
    $criteria->compare('telephone',$this->telephone,true);
    $criteria->compare('fax',$this->fax,true);
    $criteria->compare('website',$this->website,true);

    return new CActiveDataProvider(get_class($this), array(
        'criteria'=>$criteria,
        'sort'=>array(
            'defaultOrder'=>'name ASC',
        ),
        'pagination'=>array(
            'pageSize'=>20
        ),
    ));
}

Controllers/SupermarketsController:.

public function actionIndex() {
    $model =new Supermarkets('search');
    if(isset($_GET['Supermarkets']))
        $model->attributes =$_GET['Supermarkets'];

    return  $this->render('index', array('model'=>$model));
}

【讨论】:

  • yii2不能使用CDbcriteria,有什么建议吗?
  • CDbCriteria 类在 Yii 2 中被 ActiveQuery 取代。您可以使用它来构建查询。或者,您可以按照以下 wiki 中的说明设置搜索模型。 yiiframework.com/wiki/621/…
【解决方案2】:

在模型类中而不是在控制器中组织所有查询和过滤的正确和更好的方法。

您应该使用findAll() 而不是find() - 来查找符合您条件的所有记录。 find() - 仅限返回一条记录。

你可以用不同的方式做你需要的事情:

  1. 只需制作字符串 SQL-criteria 以搜索必要的属性:

    findAll('Name = "Supermarket" OR Telephone = "99988899"')

// 使用名称“Supermarket”或电话搜索超市 - “99988899”(不带引号)

  1. 使用 CDbCriteria 的良好 Yii 方式:

$criteria = 新 CDbCriteria(); // 添加要过滤的任何属性 $criteria->addCondition('Name = "Supermarket"'); $criteria->addCondition('Telephone = "99988899"','OR');

并像 findAll($criteria) 一样使用 findAll。

最后 - 只需阅读有关模型和使用 ActiveRecord 的 Yii 手册。它的文档非常好,易于理解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多