生成查询所需要的条件,排序方式

/**

*生成查询所需要的条件,排序方式

*@parammixed $searchfields查询条件

*@paramboolean $relationSearch是否关联查询

*@returnarray

*/

protected functionbuildparams($searchfields=null,$relationSearch=null)

{

$searchfields= is_null($searchfields) ?$this->searchFields:$searchfields;

$relationSearch= is_null($relationSearch) ?$this->relationSearch:$relationSearch;

$search=$this->request->get("search",'');

$filter=$this->request->get("filter",'');

$op=$this->request->get("op",'');

$sort=$this->request->get("sort","id");

$order=$this->request->get("order","DESC");

$offset=$this->request->get("offset",0);

$limit=$this->request->get("limit",0);

$filter= json_decode($filter,TRUE);

$op= json_decode($op,TRUE);

$filter=$filter?$filter: [];

$where= [];

$modelName='';

if($relationSearch)

{

if(!empty($this->model))

{

$class= get_class($this->model);

$name= basename(str_replace('\\','/',$class));

$name= strtolower(preg_replace('/(?

$modelName=$name.".";

}

if(stripos($sort,".") ===false)

{

$sort=$modelName.$sort;

}

}

if($search)

{

$searcharr= is_array($searchfields) ?$searchfields: explode(',',$searchfields);

$searchlist= [];

foreach($searcharras$k=>$v)

{

$searchlist[] = (stripos($v,".") !==false?$v:"{$modelName}`{$v}`") ." LIKE '%{$search}%'";

}

$where[] ="(". implode(' OR ',$searchlist) .")";

}

foreach($filteras$k=>$v)

{

$sym=isset($op[$k]) ?$op[$k] :'=';

if(stripos($k,".") ===false)

{

$k=$modelName.$k;

}

$sym=isset($op[$k]) ?$op[$k] :$sym;

switch($sym)

{

case'=':

case'!=':

case'LIKE':

case'NOT LIKE':

$where[] = [$k,$sym,$v];

break;

case'>':

case'>=':

case'<':

case'<=':

$where[] = [$k,$sym,intval($v)];

break;

case'IN(...)':

case'NOT IN(...)':

$where[] = [$k,str_replace('(...)','',$sym),explode(',',$v)];

break;

case'BETWEEN':

case'NOT BETWEEN':

$where[] = [$k,$sym,array_slice(explode(',',$v),0,2)];

break;

case'LIKE %...%':

$where[] = [$k,'LIKE',"%{$v}%"];

break;

case'IS NULL':

case'IS NOT NULL':

$where[] = [$k,strtolower(str_replace('IS ','',$sym))];

break;

default:

break;

}

}

$where=function($query)use($where) {

foreach($whereas$k=>$v)

{

if(is_array($v))

{

call_user_func_array([$query,'where'],$v);

}

else

{

$query->where($v);

}

}

};

return[$where,$sort,$order,$offset,$limit];

}

你可能感兴趣的:(生成查询所需要的条件,排序方式)