在我的应用程序中有一个dataTable,所以有搜索机制,dataTable中的一个列有一个int数据类型。以下是我进行服务器端搜索的方法:
private function _get_datatables_query()
{
$this->db->from($this->table);
$i = 0;
foreach ($this->column_search as $item) // loop column
{
if($_POST['sSearch']) // if datatable send POST for search
{
if($i===0) // first loop
{
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item.'::varchar', $_POST['sSearch']);
}
else
{
$this->db->or_like($item.'::varchar', $_POST['sSearch']);
}
if(count($this->column_search) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$i++;
}
if(isset($_POST['iSortCol_0'])) // here order processing
$this->db->order_by($this->column_order[$_POST['iSortCol_0']], $_POST['sSortDir_0']);
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
在运行时我遇到错误,因为演员表是这样写的:"some_column::varchar" like '%search_data%'
;它应该是这样的:"some_column"::varchar like '%search_data%'
那么在调用$this->db->like
时如何投射列?
答案 0 :(得分:0)
当列是相关列时,我发现了一个to_char
的技巧:
foreach ($this->column_search as $item) // loop column
{
if($_POST['sSearch']) // if datatable send POST for search
{
if($i===0) // first loop
{
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
if ($item == 'ladate')
$item = "ltrim(to_char(ladate,'99999999'))";
$this->db->like($item, $_POST['sSearch']);
}
else
{
if ($item == 'ladate')
$item = "ltrim(to_char(ladate,'99999999'))";
$this->db->or_like($item, $_POST['sSearch']);
}
if(count($this->column_search) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$i++;
}