Welcome Guest, Not a member yet? Register   Sign In
Help about query
#1

[eluser]Wondering Coder[/eluser]
I like the part of CI active record where I can have a dynamic where clause using get_where().
Code:
My controller
$cond = array('user_id'=>1,'data_id'=>2)
$this->model->get_logged_user($cond);

My model
function get_logged_user($data)
    {
        $this->db->select('user_id, data_id, coor_id, image');
        $query = $this->db->get_where('users',$data);
        return $query;

but now I'm using a subquery and since CI active record doesn't support subquery, I write my query like this below.
Code:
function get_logged_user($userid,$dataid)
    {
        $query = $this->db->query("SELECT user_id, data_id, coor_id, image FROM (
                                   SELECT ad_userid user_id, ad_dataid data_id, '' coor_id, image FROM pps_admin_users
                                    UNION
                                   SELECT user_id, data_id, coor_id, image FROM pps_users) b
                                  
                                   WHERE b.user_id=$userid AND b.data_id=$dataid",FALSE);
        
        return $query;
    }
but now my problem is how can I have the same functionality in get_where without using CI active. coz I'll be using again this query but with different where clause. Anyone have an idea here?
#2

[eluser]jmadsen[/eluser]
make your query by a string you plug in:

Code:
$sql = "blah blah blah";
$query = $this->db->query($sql);
#3

[eluser]Wondering Coder[/eluser]
thanks for replying jmadsen,

I don't think that's what I'm looking for. I want a dynamic where clause just like using get_where but can't use CI active record though coz of my subquery.
#4

[eluser]Aken[/eluser]
I would either extend the database class or create a helper function using the code that active record uses to put together WHERE statements.




Theme © iAndrew 2016 - Forum software by © MyBB