CodeIgniter Forums
Help about query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Help about query (/thread-43625.html)



Help about query - El Forum - 07-18-2011

[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?


Help about query - El Forum - 07-18-2011

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

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



Help about query - El Forum - 07-19-2011

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


Help about query - El Forum - 07-20-2011

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