首页 > 其他 > 详细

yii2:多条件多where条件下碰到between时,between语句如何处理呢?

时间:2017-04-06 15:33:09      阅读:2219      评论:0      收藏:0      [点我收藏+]

yii2:多条件多where条件下碰到between时,between语句如何处理呢?

我有一张表:
id,name,telphone,ticket_no,status,create_time等字段,

在出具多条件查询时(当不涉及到时间范围或其他范围),可以用如下语句:

if (!empty($params[‘id‘])) {
    		$where_condition[‘oid‘] = $params[‘id‘];            
    	}
    	if (!empty($params[‘post_name‘])) {
    		$where_condition[‘post_name‘] = $params[‘post_name‘];           
    	}
    	if (!empty($params[‘telephone‘])) {
    		$where_condition[‘telephone‘] = $params[‘telephone‘];           
    	}
    	if (!empty($params[‘ticket_no‘])) {
    		$where_condition[‘ticket_no‘] = $params[‘ticket_no‘];           
    	}
    	if ($params[‘status‘] != -1) {
    		$where_condition[‘status‘] = $params[‘status‘];            
    	}  
        $where_condition[‘delete_flg‘] = 0;   
    	$count = static::find()
    	->where($where_condition)
           ->andFilterWhere([‘between‘,‘CREATE_TIME‘,$start_date, $end_date])
    		->count();

  

当涉及到create_time时间范围查询时,那么问题来了,between怎么加进去?$where_condition[‘CREATE_TIME‘]=‘between 时间1 and 时间2‘ 这样是不行的,花了一点时间查询了下,yii2有这样的方法:andFilterWhere,使用方法如下:

->andFilterWhere([‘like1‘, ‘name‘, ‘%a%‘])

#当参数1,参数2为空时,between方法会自动过滤掉,也就是此条件会被删除不执行
 ->andFilterWhere([‘between‘, ‘created_at‘, 0(参数1), 1433088000(参数2)])

  

 

具体代码如下:

if (!empty($params[‘id‘])) {
    		$where_condition[‘oid‘] = $params[‘id‘];
            
    	}
    	if (!empty($params[‘post_name‘])) {
    		$where_condition[‘post_name‘] = $params[‘post_name‘];
            
    	}
    	if (!empty($params[‘telephone‘])) {
    		$where_condition[‘telephone‘] = $params[‘telephone‘];
           
    	}
    	if (!empty($params[‘ticket_no‘])) {
    		$where_condition[‘ticket_no‘] = $params[‘ticket_no‘];
          
    	}
    	if ($params[‘status‘] != -1) {
    		$where_condition[‘status‘] = $params[‘status‘];
           
    	}
        $start_date = $end_date = ‘‘;
        if($params[‘isSearch‘] == 1) {
            if (!empty($params[‘start_date‘]) && !empty($params[‘end_date‘])) {
                $start_date = strtotime($params[‘start_date‘]);
                $end_date = strtotime($params[‘end_date‘]) + 86400 - 1;
              
            }
        }
        $where_condition[‘delete_flg‘] = 0;
       
    	
    		$count = static::find()
    		->where($where_condition)
            ->andFilterWhere([‘between‘,‘CREATE_TIME‘,$start_date, $end_date])
    		->count();

  

 

yii2:多条件多where条件下碰到between时,between语句如何处理呢?

原文:http://www.cnblogs.com/achengmu/p/6673566.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!