I have my function to get a list of departments where i do a left join from 2 tables, i want to know how can i make the equivalent on codeigniter way? my code is this:
function get_department_list()
{
$sql = '
SELECT
a.employee_name,
b.department_name
FROM
tbl_employee a
LEFT JOIN tbl_department b ON b.department_id = a.department_id';
$query = $this->db->query($sql);
$result = $query->result();
return $result;
}
I was reading the codeigniter documentation and tried to do it the CI way but i only get errors, this is how i tried to do it:
function get_department_list()
{
$sql = '
$this->db->select('a.employee_name','b.department_name');
$this->db->from('tbl_employee a');
$this->db->join('tbl_employee b', 'b.department_id = a.department_id', 'left');
$query = $this->db->get();
$query = $this->db->query($sql);
$result = $query->result();
return $result;
}
This is the error:
Parse error: syntax error, unexpected 'a' (T_STRING) in /Applications/MAMP/htdocs/CI-Employee/application/models/Department_model.php on line 13 A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected 'a' (T_STRING)
Filename: models/Department_model.php
Line Number: 13
Backtrace:
Any help will be really appreciated thanks guys!
EDIT:
This was my solution and thanks guys for your time!
function get_department_list()
{
$this->db->select('a.employee_name,b.department_name');
$this->db->from('tbl_employee a');
$this->db->join('tbl_department b', 'b.department_id = a.department_id', 'left');
$query = $this->db->get();
return $query->result();
}