Welcome Guest, Not a member yet? Register   Sign In
How would you write given query using active record
#1

[eluser]Unknown[/eluser]
query
Code:
SELECT * FROM `users`
WHERE `class` = $user
AND (`method` = 0 OR `method` = $method)
AND (

     ((`role` = 0 OR `role` = $role) AND  `user_id` = 0)
     OR
     ((`user_id` = 0 OR `user_id` = $user_id) AND `role` = 0)

     )

Im not shure how to "combine" (`role` = 0 OR `role` = $role)

im stuck Smile
#2

[eluser]Dennis Rasmussen[/eluser]
I wouldn't.
AR is just for easy and quick queries (to my understanding).
#3

[eluser]smilie[/eluser]
Although I do agree with Dennis (using AR for 'simple' queries), I think AR could do this as well with: or_where:

Quote:$this->db->or_where();

This function is identical to the one above, except that multiple instances are joined by OR:

$this->db->where('name !=', $name);
$this->db->or_where('id >', $id);

// Produces: WHERE name != 'Joe' OR id > 50
Note: or_where() was formerly known as orwhere(), which has been deprecated.

See: http://ellislab.com/codeigniter/user-gui...tml#select (and then a bit bellow on that page).

So, it should be doable Smile

Cheers,
Smilie
#4

[eluser]Cristian Gilè[/eluser]
Dennis, you are right. AR is for quick and easy queries.

In the CI reactor forum (http://bit.ly/fABQQk) there are some votes to add the parentheses in the AR.

In the meantime, take a look at this post: http://bit.ly/e66mbQ
#5

[eluser]Dennis Rasmussen[/eluser]
Sure it's doable with AR, but there's really no need to.
I wouldn't recommend it (considering all the "hard" work you'll have to do compared to already having the solution as a standard query) and that's why I didn't give an example or reference to a solution.

Sometimes simple is best.
#6

[eluser]Unknown[/eluser]
thanks for helping.. I went with using "ordinary" queries with binding variables.




Theme © iAndrew 2016 - Forum software by © MyBB