Q:

Codeigniter 2.1 - active record

I have reached dead end with the brain o.O. In DB I have two tables:
store_module->caffe_id, module_id, position, order 
module->id_module, name, description, image
I have query where I take all modules for set ID (store_module table), and I need to get all modules which appear in this query (module_id). What I need to do? 
This is the code (I am awake for 30+ hours and my brain is refusing to communicate with me, deadline is almost here, and this on of the last things I need to do. So, please help :D): 
function mar_get_modules($id){
            $q = $this->db->get_where('store_module', array('caffe_id' => $id));
            $modules = $q->result_array();
}

A:

Start simple, by using a regular query (if I guess right, you need a JOIN there).
This query should work:
$sql = "SELECT m.*,sm.* FROM module m
       LEFT JOIN store_module sm ON sm.id_module = m.module_id
       WHERE sm.caffe_id = ?";
return $this->db->query($sql, array($id))->result_array();

Now, you can transform it into an AR query:
$query = $this->db->select('module.*,store_module.*')
                  ->from('module')
                  ->join('store_module', 'store_module.id_module = module.module_id','left')
                  ->where('store_module.caffe_id',$id)
                  ->get();
return $query->result_array();

While AR is quicker sometimes, I usually prefer writing my queries "by hand", taking advantage of the binding to prevent SQL injections; it's a lot easier to see how things are working if you have a query fully laid under your eyes

