Welcome Guest, Not a member yet? Register   Sign In
active record doubt
#1

[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!!
#2

[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);
#3

[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.
#4

[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.
#5

[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');
#6

[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.
#7

[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');
#8

[eluser]jparent[/eluser]
@Aken

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




Theme © iAndrew 2016 - Forum software by © MyBB