简体   繁体   English

在Yii CGridView上进行多列搜索

[英]Multi column search on Yii CGridView

I have admin page contain CGridView and connected with user database, I need to make search on CGridView work on multi column not just one.. like this: 我的管理页面包含CGridView并与用户数据库连接,我需要在CGridView上进行多列搜索,而不仅仅是一个..像这样:

I need to find user information by phone number OR user name OR email address, is that possible ? 我需要通过电话号码或用户名或电子邮件地址查找用户信息,这可能吗? and my code is under the image: 我的代码在图片下:

在此处输入图片说明

Code

User model: I used OR, but it's not work.. I still search by name only ! 用户模型:我使用了OR,但不起作用。我仍然仅按名称搜索!

public function search()
{
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('full_name',$this->full_name,'OR');
    $criteria->compare('full_name',$this->landline,'OR');
    $criteria->compare('full_name',$this->mobile_number,'OR');
    $criteria->compare('email_address',$this->email_address,true);
    $criteria->compare('city',$this->city,true);


    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
        'sort'=>array(
            'defaultOrder'=>'id DESC',
        )
    ));
}

You made little mistakes comparing 您比较时犯了小错误

$criteria->compare('full_name', $this->full_name, true, 'OR');
$criteria->compare('landline', $this->full_name, true, 'OR');
$criteria->compare('mobile_number', $this->full_name, true, 'OR');

First parameter is field in a database and second is what you filled in a form. 第一个参数是数据库中的字段,第二个参数是您填写的表单。 You also skipped third parameter which is to allow partial matching. 您还跳过了第三个允许部分匹配的参数。 And the forth parameter needs to be 'OR' 并且第四个参数需要为“ OR”

Keep in mind that this is work around, and it can give some headaches later on if you are not careful, because you are storing landline and mobile_phone into $model->full_name . 请记住,这是可以解决的,如果您不小心的话,稍后可能会有些头疼,因为您将landlinemobile_phone存储在$model->full_name

I would suggest you add another property to User mode, something like user_info and than use that property to store input from request and compare it with database. 我建议您向用户模式添加另一个属性,例如user_info然后使用该属性存储来自请求的输入并将其与数据库进行比较。

Example from above will still work, if you wish to improve your code with additional parameter and you can't find your way with it, I'll explain it better, just shout. 上面的示例仍然有效,如果您希望通过附加参数来改进代码,但是找不到自己的方式,我会更好地解释,只是大声喊叫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM