CodeIgniter Forums

Full Version: active record doubt
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]jparent[/eluser]
Hi I try make a sentence like

select * from table where num_field = search_word OR text_field like %search_word%
if I do:

$this->db->or_where('num_field',$search_word);
$this->db->or_like('text_field',$search_word);

it makes a sentence like:

select * from table where num_field = search_word AND text_field like %search_word%

to solve it I use that:

$this->db->or_where("num_field = search_word OR text_field like %search_word%");

but that implies use SQL sintax and lossing de Abstraction.

Is there other way to construct this SQL with active record and no write SQL? Thanks!!

El Forum

[eluser]kurucu[/eluser]
Should it not be as follows?
Code:
$this->db->where(‘num_field’,$search_word);
$this->db->or_like(‘text_field’,$search_word);

OR

Code:
$this->db->like(‘text_field’,$search_word);
$this->db->or_where(‘num_field’,$search_word);

El Forum

[eluser]überfuzz[/eluser]
First of all, have a good look at the manual.

Hints: Add a bit of ...db->select() db->from() etc and you'll get it working.

El Forum

[eluser]jparent[/eluser]
@überfuzz

I don't know how add db->select() db->from() will change the OR sentence between 'where' and 'like' by an AND sentence. Could you refer the page of manual that explains that? Thank you! Wink

@kurucu

No it doesn't work. I tried it. It makes select * from table where num_field = search_word AND text_field like %search_word%

beacause for de AR class wheres and like are differnt and always join them with an AND.

El Forum

[eluser]Aken[/eluser]
or_where and or_like need to be used in conjunction with a where/like respectively to add the OR functionality. You can use a custom query string inside the where() function to get what you need.

Code:
$this->db->where("num_field = 'search_word' OR text_field LIKE '%search_word%'", NULL, FALSE);
$this->db->get('table');

El Forum

[eluser]jparent[/eluser]
@Aken

Yes, that's the solution I find but this solution implies write SQL specific sintax like %, That I ask at the begin of the post is if there is a sequece of lines of code that do de same that you write but using only active record methods that not imply write SQL sintax, only write constants, variables or field or table names.

Thanks.

El Forum

[eluser]Aken[/eluser]
After a little research, you should be able to use a LIKE command with no wild cards just as you would a "WHERE = something" command. However, there's no option to use the $this->db->like() function without any wildcard placement.

You can, however, use a very minor custom query string inside an or_where() function:

Code:
$search = 'search_word';

$this->db->where('num_field', $search);
$this->db->or_where('text_field LIKE', "%$search%");
$query = $this->db->get('table');

El Forum

[eluser]jparent[/eluser]
@Aken

Thanks! That's excellent idea! I put it in practice!!