CodeIgniter Forums
How to join three or more tables? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to join three or more tables? (/showthread.php?tid=9251)



How to join three or more tables? - El Forum - 06-18-2008

[eluser]doors[/eluser]
I want to create an array with data from three tables.

How would I go about doing that using the active record class?

It works with two tables easily but when I add anymore tables I get some errors.

Can someone place a simple example?


How to join three or more tables? - El Forum - 06-18-2008

[eluser]ch5i[/eluser]
Here is a join over three tables:

device <-> device_type <-> lang_device_type

Code:
function getDevicesByUnitId($unit_id)
    {
        $this->db->select('*, ldt.name as typename, d.name as devicename');
        $this->db->orderby('d.name', 'asc');
        $this->db->join('device d', 'd.device_type_id = dt.device_type_id', 'inner');
        $this->db->join('lang_device_type ldt', 'ldt.device_type_id = dt.device_type_id', 'inner');
        $this->db->where('d.unit_id', $unit_id);
        $this->db->where('ldt.lang_id', $this->session->userdata('lang'));
        return $this->db->get('device_type dt');
    }