Welcome Guest, Not a member yet? Register   Sign In
AND and ActiveRecords
#1

[eluser]emurad[/eluser]
Hello guys,

An active record is generating the following:

Code:
SELECT `items`.*, (select users.username from users where users.id = items.user_id) as username,
`categories`.`title` as cat_title, `categories`.`friendly_id` as cat_friendly_id FROM (`items`)
JOIN `categories` ON `categories`.`id` = `items`.`category_id` WHERE `items`.`type` = 'books'
AND `items`.`category_id` = '11' AND `items`.`status` = 1 AND `items`.`title` LIKE '%www%'
OR `items`.`author` LIKE '%www%' OR `items`.`description` LIKE '%www%'
OR `items`.`content` LIKE '%www%' OR `items`.`additional` LIKE '%www%'
ORDER BY `items`.`id` DESC LIMIT 10

I need it as follow:

Code:
SELECT `items`.*, (select users.username from users where users.id = items.user_id) as username,
`categories`.`title` as cat_title, `categories`.`friendly_id` as cat_friendly_id FROM (`items`)
JOIN `categories` ON `categories`.`id` = `items`.`category_id` WHERE `items`.`type` = 'books'
AND `items`.`category_id` = '11' AND `items`.`status` = 1 AND ******(******`items`.`title` LIKE '%www%'
OR `items`.`author` LIKE '%www%' OR `items`.`description` LIKE '%www%'
OR `items`.`content` LIKE '%www%' OR `items`.`additional` LIKE '%www%'******)******
ORDER BY `items`.`id` DESC LIMIT 10

Note the brakets emphasized by the 6 asterisks. Of course I just want to add the brakets not the asterisks.

Thanks.
#2

[eluser]Armchair Samurai[/eluser]
You'll need to do something like this for the brackets:

Code:
$var = $this->db->escape_like_str('www');
$sql = <<<ENDSQL
(
    items.title LIKE %$var%
    OR items.author LIKE %$var%
    OR items.description LIKE %$var%
    OR items.content LIKE %$var%
    OR items.additional LIKE %$var%
)
ENDSQL;

// Active record stuff

$this->db->where($sql, NULL, FALSE);

// Continue active record stuff




Theme © iAndrew 2016 - Forum software by © MyBB