与另一个表连接的ion_auth活动记录/方法

时间:2016-03-31 06:02:00

标签: mysql codeigniter ion-auth

我现在正在使用ion_auth, 在ion_auth中,我们可以选择所有这样的记录:

控制器

 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');
}

我的情况是,我在默认表ion_auth中添加了一个名为id_departement的列,这是另一个表中的foregin键,如下所示:

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)

这是ion_auth表:

MariaDB [ittresnamuda]> select id, username, email, id_departement from users LIMIT 20;
+----+---------------+--------------------------+----------------+
| id | username      | email                    | id_departement |
+----+---------------+--------------------------+----------------+
|  1 | administrator | admin@admin.com          |           NULL |
|  5 | Dzil          | dzil@tresnamuda.co.id    |              1 |
| 19 | David         | david_vl@indo.net.id     |              3 |
| 20 | Emmy          | emmy@indo.net.id         |              5 |
| 21 | Hendrik       | hl_kong@indo.net.id      |              3 |
| 22 | ATaufiq       | taufik@tresnamuda.co.id  |              1 |
| 23 | Awal          | awal@tresnamuda.co.id    |              1 |
| 24 | Riana         | riana@tresnamuda.co.id   |              1 |
| 25 | Nugroho       | accjkt@tresnamuda.co.id  |              1 |
| 26 | Dudung        | accjkt@tresnamuda.co.id  |              1 |
| 27 | Farida        | accjkt@tresnamuda.co.id  |              1 |
| 28 | Nita          | accjkt2@tresnamuda.co.id |              1 |
| 29 | Rayen         | accjkt2@tresnamuda.co.id |              1 |
| 30 | Taka          | accjkt@tresnamuda.co.id  |              1 |
| 31 | Anjar         | accjkt2@tresnamuda.co.id |              1 |
| 32 | Toni          | accjkt@tresnamuda.co.id  |              1 |
| 33 | Sukardi       | sukardi@tresnamuda.co.id |              2 |
| 34 | Tiur          | tiur@tresnamuda.co.id    |              2 |
| 35 | Yuti          | amdjkt@tresnamuda.co.id  |              2 |
| 36 | Erfin         | erfin@tresnamuda.co.id   |              2 |
+----+---------------+--------------------------+----------------+

20行(0.00秒)

如何使用ion_auth活动记录获取name_departement和别名?

1 个答案:

答案 0 :(得分:1)

使用钩子,您可以在调用ion_auth的用户方法之前修改数据库查询,因此它将与您的表一起加入,如下所示:

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');
    }
}