You can do it with activerecord like this:
$this->-db->select('page.*,role_page_Access.access_level');
$this->db->from('page');
$this->db->join('role_page_access', 'role_page_access.page_id = page.id', 'left outer');
$this->db->where('role_page_access.role_id', $role_id);
$query = $this->db->get();
foreach($query->result() as $row) {
if(empty($row->access_level)) {
echo 'Role does not have access';
} else {
echo 'Role has access';
}
}
That will join the page
table records with the role_page_access
table records by the page.id
field if it matches, filtered by the role_id
you pass along.
You can even do method chaining if you have a supported PHP version, check the reference here.
Update
If you still want to retrieve page records even if that role_id
does not have access you just have to set the join as left outer:
$this->db->join('role_page_access', 'role_page_access.page_id', 'left outer');
That'll give ALL pages records, even if they don't match a role on role_page_access
and the resultset object will have a NULL
value on role_page_access
. I updated the original code to show you an example.
Update 2
If you want to filter by role_id
but still have the unmatched records:
$this->db->where('role_page_access.role_id', $role_id);
$this->db->or_where('role_page_access.role_id', NULL);
But you will have to filter them by the access_level
to check if is NULL
cause if it is, it means that there was no matching page for the page_id
and the role_id
(that you passed as param) combination: