Reputation: 2206
I am using ion_auth now, In ion_auth, we can select all record like this :
Controller
public function index($group_id = NULL) {
$before_head = $this->load->view('admin/users/v_before_head', '', TRUE);
$before_body = $this->load->view('admin/users/v_before_body', '', TRUE);
$this->data['page_title'] = 'Users';
$this->data['before_head'] = $before_head;
$this->data['before_body'] = $before_body;
$this->data['users'] = $this->ion_auth->users($group_id)->result();
$this->render('admin/users/v_list_users');
}
My case is, I added one more column in default table ion_auth named id_departement which is a foregin key from another table like this :
MariaDB [ittresnamuda]> select * from tb_departement;
+----------------+------------------+-------+
| id_departement | nama_departement | alias |
+----------------+------------------+-------+
| 1 | ACCOUNTING | ACC |
| 2 | ARMADA | AMD |
| 3 | DIREKSI | DIR |
| 4 | EKSPOR | EXP |
| 5 | FINANCE | FIN |
| 6 | IMPORT | IMP |
| 7 | IT | IT |
| 8 | INVENTORY | INV |
| 9 | MARKETING | MKT |
| 10 | PERSUM | HRD |
| 11 | PURCHASING | PRC |
| 12 | OPERATION | OPS |
| 13 | LEGAL & CLAIM | LCL |
| 14 | SEKRETARIAT | SKRT |
+----------------+------------------+-------+
14 rows in set (0.00 sec)
This is the ion_auth table :
MariaDB [ittresnamuda]> select id, username, email, id_departement from users LIMIT 20;
+----+---------------+--------------------------+----------------+
| id | username | email | id_departement |
+----+---------------+--------------------------+----------------+
| 1 | administrator | [email protected] | NULL |
| 5 | Dzil | [email protected] | 1 |
| 19 | David | [email protected] | 3 |
| 20 | Emmy | [email protected] | 5 |
| 21 | Hendrik | [email protected] | 3 |
| 22 | ATaufiq | [email protected] | 1 |
| 23 | Awal | [email protected] | 1 |
| 24 | Riana | [email protected] | 1 |
| 25 | Nugroho | [email protected] | 1 |
| 26 | Dudung | [email protected] | 1 |
| 27 | Farida | [email protected] | 1 |
| 28 | Nita | [email protected] | 1 |
| 29 | Rayen | [email protected] | 1 |
| 30 | Taka | [email protected] | 1 |
| 31 | Anjar | [email protected] | 1 |
| 32 | Toni | [email protected] | 1 |
| 33 | Sukardi | [email protected] | 2 |
| 34 | Tiur | [email protected] | 2 |
| 35 | Yuti | [email protected] | 2 |
| 36 | Erfin | [email protected] | 2 |
+----+---------------+--------------------------+----------------+
20 rows in set (0.00 sec)
How can I get name_departement and alias using ion_auth active record ?
Upvotes: 0
Views: 131
Reputation: 686
With hooks you can modify the db query before you call the ion_auth's users method, so it will join with your table, like this:
class MyController extends CI_Controller {
// callback function to add join to db before call users method
public function prepare_user_list() {
$this->db->join('tb_departement','tb_departement.id_departement = ' . 'users.id_departement','inner');
}
public function index($group_id = NULL) {
$before_head = $this->load->view('admin/users/v_before_head', '', TRUE);
$before_body = $this->load->view('admin/users/v_before_body', '', TRUE);
$this->data['page_title'] = 'Users';
$this->data['before_head'] = $before_head;
$this->data['before_body'] = $before_body;
// subscribe for the users event
$this->ion_auth->set_hook('users', 'prepare_user_list', $this, 'prepare_user_list', array());
// then query the users
$this->data['users'] = $this->ion_auth->users($group_id)->result();
$this->render('admin/users/v_list_users');
}
}
Upvotes: 1