Codeigniter : Error in ORDER BY CASE query

前端 未结 5 1360
野性不改
野性不改 2021-01-05 12:10

Here is my query in Codeigniter

$this->db->select(\'p.*,u.firstname, u.lastname,s.title AS industry, pt.type_name , al.length_value\',         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 12:35

    I figured it out how to avoid escaping CASE word.

    You can modify $_reserved_identifiers variable inside CI_DB_driver class. If you don't want to modify whole file, you can simply change it inside your Model class just before that query.

    $this->db->_reserved_identifiers = array('*','CASE');
    
    $this->db->select('p.*,u.firstname, u.lastname,s.title AS industry, pt.type_name , al.length_value',FALSE);
    $this->db->from($this->_tbl_projects . ' as p');
    $this->db->join($this->_tbl_client_details . ' as c', 'c.id = p.client_id', 'left');
    $this->db->join($this->_tbl_users . ' as u', 'u.id = c.user_id', 'left');
    $this->db->join($this->_tbl_project_types . ' as pt', 'pt.project_type_id = p.project_type_id', 'left');
    $this->db->join($this->_tbl_specializations . ' as s', 's.specialization_id = p.specialization_id', 'left');
    $this->db->join($this->_tbl_article_length . ' as al', 'al.article_length_id = p.article_length_id', 'left');
    $this->db->order_by("CASE p.submit_to
                                    WHEN '' THEN 0
                                    WHEN 'writer' THEN 1
                                    ELSE 2
                                END, p.request_end_date asc",FALSE);
    

    (I don't know if it's working on 3.x branch. It's working good on 2.x branch)

提交回复
热议问题