• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can not use Active Record for simple query?

#1
[eluser]victorche[/eluser]
I need a really simple query with 2 OR and 1 AND like this:
Code:
"SELECT * " .
            "FROM messages " .
            "WHERE (receiver_id = {$user_id} OR " .
                "creator_id = {$user_id} OR " .
                "receiver_id = 0) AND " .
                "deleted = 1"
Trying this with Active Record does not work, since I can not add the brackets. Maybe I am doing it wrong... This is my (not working) code:
Code:
$this->db->from('messages')
                ->where('receiver_id', $user_id)
                ->or_where('creator_id', $user_id)
                ->or_where('receiver_id', 0)
                ->where('deleted', 1)
                ->get();
Can someone tell me how to do it?

#2
[eluser]Dennis Rasmussen[/eluser]
If it's too complicated to do with AR and you already have the query, then there's no reason to use AR?!
AR is just meant to be used as a quick query builder.

#3
[eluser]seeraw[/eluser]
Hi

I tested above query with my own conditions like

$this->db->select('*');
$this->db->from('TABLE_NAME');

$this->db->where('column name','value'); //condition one
$this->db->or_where('column name','value'); //condition tow
$this->db->where('column name','value'); //condition three
$this->db->or_where('column name','value'); //condition four
$this->db->get();

and it's working fine (it changes the sequence of "AND" and "OR" in your query other wise no problem with this query ),

what output you are getting with your query?

Thanks,
Swapnil

#4
[eluser]Dennis Rasmussen[/eluser]
[quote author="seeraw" date="1299254430"]Hi

I tested above query with my own conditions like

$this->db->select('*');
$this->db->from('TABLE_NAME');

$this->db->where('column name','value'); //condition one
$this->db->or_where('column name','value'); //condition tow
$this->db->where('column name','value'); //condition three
$this->db->or_where('column name','value'); //condition four
$this->db->get();

and it's working fine (it changes the sequence of "AND" and "OR" in your query other wise no problem with this query ),

what output you are getting with your query?

Thanks,
Swapnil[/quote]

Did you see his brackets?

#5
[eluser]seeraw[/eluser]
[quote author="Dennis Rasmussen" date="1299254749"][quote author="seeraw" date="1299254430"]Hi

I tested above query with my own conditions like

$this->db->select('*');
$this->db->from('TABLE_NAME');

$this->db->where('column name','value'); //condition one
$this->db->or_where('column name','value'); //condition tow
$this->db->where('column name','value'); //condition three
$this->db->or_where('column name','value'); //condition four
$this->db->get();

and it's working fine (it changes the sequence of "AND" and "OR" in your query other wise no problem with this query ),

what output you are getting with your query?

Thanks,
Swapnil[/quote]

Did you see his brackets?[/quote]


Yes but though it is complicated it will work it will not produce any error
What I mean you can also write the same query with AR

#6
[eluser]Dennis Rasmussen[/eluser]
[quote author="seeraw" date="1299255706"][quote author="Dennis Rasmussen" date="1299254749"][quote author="seeraw" date="1299254430"]Hi

I tested above query with my own conditions like

$this->db->select('*');
$this->db->from('TABLE_NAME');

$this->db->where('column name','value'); //condition one
$this->db->or_where('column name','value'); //condition tow
$this->db->where('column name','value'); //condition three
$this->db->or_where('column name','value'); //condition four
$this->db->get();

and it's working fine (it changes the sequence of "AND" and "OR" in your query other wise no problem with this query ),

what output you are getting with your query?

Thanks,
Swapnil[/quote]

Did you see his brackets?[/quote]


Yes but though it is complicated it will work it will not produce any error
What I mean you can also write the same query with AR[/quote]
You're not making any sense, at least not to me.
How is your code answering his problem?

@victorche, AR is meant to be used for simple queries. You CAN do what you want with AR, but the work it requires is more complicated than just running $this->db->query();

#7
[eluser]seeraw[/eluser]
[quote author="Dennis Rasmussen" date="1299256001"][quote author="seeraw" date="1299255706"][quote author="Dennis Rasmussen" date="1299254749"][quote author="seeraw" date="1299254430"]Hi

I tested above query with my own conditions like

$this->db->select('*');
$this->db->from('TABLE_NAME');

$this->db->where('column name','value'); //condition one
$this->db->or_where('column name','value'); //condition tow
$this->db->where('column name','value'); //condition three
$this->db->or_where('column name','value'); //condition four
$this->db->get();

and it's working fine (it changes the sequence of "AND" and "OR" in your query other wise no problem with this query ),

what output you are getting with your query?

Thanks,
Swapnil[/quote]

Did you see his brackets?[/quote]


Yes but though it is complicated it will work it will not produce any error
What I mean you can also write the same query with AR[/quote]
You're not making any sense, at least not to me.
How is your code answering his problem?

@seeraw, AR is meant to be used for simple queries. You CAN do what you want with AR, but the work it requires is more complicated than just running $this->db->query();[/quote]

OK,
Sorry I didn't see "brackets"
And I agree that "AR is meant to be used for simple queries"
if it is the case with "brackets" we can write such queries with

$this->db->query("QUERY HERE");
Am I correct Dennis Rasmussen.

#8
[eluser]louisl[/eluser]
Not sure why you're using curly brackets there, I've hardly ever found the need for them myself, but could you set a var before the query?

$user_id = {$user_id};

#9
[eluser]Dennis Rasmussen[/eluser]
[quote author="louisl" date="1299262129"]Not sure why you're using curly brackets there, I've hardly ever found the need for them myself, but could you set a var before the query?

$user_id = {$user_id};[/quote]

It's not the curly brackets he's having a trouble with.

From http://en.wikipedia.org/wiki/Bracket:
"In the United States, "bracket" usually refers specifically to the "square" or "box" type;[1][2] in British usage it normally refers to the "round" type, which is called a parenthesis mark in American usage."

#10
[eluser]Phil Sturgeon[/eluser]
Sure you can.

Code:
$this->db
  ->where("(receiver_id = {$user_id} OR creator_id = {$user_id} OR receiver_id = 0)", NULL, FALSE)
  ->where('deleted', 1)
  ->get('messages');


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.