How to make the php codeIgniter active record group?
First, I have something in my model:
$this->db->where('status', $aStatus);
$this->db->limit($aLimit, $aOffset);
$this->db->or_like('title', $aString);
$this->db->or_like开发者_如何学运维('description', $aString);
$this->db->select('id,description,title,fee');
$query = $this->db->get($this->table_name);
And I got this query:
SELECT
id
,description
,title
,fee
FROM (events
) WHEREstatus
= 0 ANDtitle
LIKE '%space%' ORdescription
LIKE '%space%' LIMIT 5
But I would like to let it generate this query instead
SELECT
id
,description
,title
,fee
FROM (events
) WHEREstatus
= 0 AND (title
LIKE '%space%' ORdescription
LIKE '%space%') LIMIT 5
How can I modify to do so? Thank you.
Think the only way you'll manage that is by doing something along the lines of:
$this->db->where('status', $aStatus);
$this->db->where("(title LIKE '%space%' OR description LIKE '%space%')");
$this->db->limit($aLimit, $aOffset);
$this->db->select('id,description,title,fee');
$query = $this->db->get($this->table_name);
Haven't tested it but it should get you on the way. Other than that I don't think it's possible to include a grouped/bracketed query any other way.
Grouping LIKE clause (putting parenthesis around)
$this->db->group_start();
$this->db->like('name', $query_array['q']);
$this->db->or_like('description', $query_array['q']);
$this->db->group_end();
Will produce round backets like this
(
`name` LIKE '%test%' ESCAPE '!'
OR `description` LIKE '%test%' ESCAPE '!'
)
I would just do something like
$this->db->where('status', $aStatus);
$this->db->limit($aLimit, $aOffset);
$this->db->where(sprintf(
'(title LIKE %s OR description LIKE %s)',
$this->db->escape_like_str($aString),
$this->db->escape_like_str($aString)
), NULL);
$this->db->select('id,description,title,fee');
$query = $this->db->get($this->table_name);
精彩评论