Welcome Guest, Not a member yet? Register   Sign In
Activerecord with multiple joins. Help
#1

[eluser]Unknown[/eluser]
I am building an application to teach myself Codeigniter. I would like some help converting a sql query into active record directives.

I have a query that I have got to work using the basic database methods in codeigniter. I would like to understand the active record structure and learn more about this, so i can use the active record structure (for learning and for code consistency)

I have a list of users in a table called UserList and there are 2 other tables that contain information relating to aspects of my application.

Ultimately what I would like is to get all the fields from the UserList and then extra columns on the end with the Category Name (from the CategoryList table) and the Time of the last user action (from the UserTimeLog table)

My solution is working at the moment (possible returning too much data,) but the tables are not going to be very big... probably less than 200 users and maybe 8 to 10 different categorites to choose from. The UserTimeLog could potentially have 1000's of records to search but that will be the only consideration as far as streamlining my query goes. I don't mind getting extra fields in the return array.

Code:
$sql="SELECT * FROM (UserList LEFT JOIN CategoryList ON UserList.Category = CategoryList.CategoryID) LEFT JOIN UserTimeLog ON UserList.LastAction = UserTimeLog.EventID";

$query = $this->db->query($sql);
return $query->result_array();

I have tried several methods of doing this with active record with no measure of success.
I deleted the code that I had written out of frustration and re-pasted the working function into my model to get my application working again. Any advice or help would be very much appreciated.
#2

[eluser]Unknown[/eluser]
I have actually stuck with the active record method and figured it out. With the help of the following methods.

Code:
$this->db->_compile_select();  
    $query=$this->db->get();
    $sql = $this->db->last_query();
    echo $sql;

This allowed me to see the actual query that active record produces. I was then able to work backwards and get the exact query I wanted to work.

The following snippet is the contents of a function in a model within my application.

Code:
$this->db->select('UserList.*, CategoryList.CatName, UserTimeLog.Time');
    $this->db->from('UserList');
    $this->db->join('CategoryList','UserList.Category = CategoryList.CategoryID','left');
    $this->db->join('UserTimeLog','UserList.LastAction = UserTimeLog.EventID','left');
    $query=$this->db->get();
    if($query->num_rows()>0){
      // return result set as an associative array
      return $query->result_array();
    }

It is working perfectly and returns ALL the fields from the table UserList and also the corresponding Catagory name from the CategoryList table as well as the actual time of the last log-in from the UserTimeLog table. Awesome




Theme © iAndrew 2016 - Forum software by © MyBB