首页 > 代码库 > yii2:多条件多where条件下碰到between时,between语句如何处理呢?

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

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语句如何处理呢?