首页 文章

Codeigniter Tank_auth添加字段

提问于
浏览
0

我使用CI tank_auth库来获取用户的注册表单,并且需要添加tank_auth没有附带的新字段 .

我确实提到了这个Tank Auth Adding Fields,但它从来没有帮助我解决我的疑问 .

让's say, I want to have additional ' Name '字段在注册期间,我在视图中创建了它并且 function register() 控制器,这样可以正常工作,除非我想把它放在 user_profiles 表中,我创建了一个名为 name 的列,然后在tank_auth模型的users.php中,我发现以下方法并添加:

private function create_profile($user_id, $name)
{
    $this->db->set('user_id', $user_id);
    $this->db->set('name', $name);
    return $this->db->insert($this->profile_table_name);
}

当我运行注册时,没有发生错误,我检查表, name 字段没有插入,所以我猜它可能没有参数传递给 $name ,这是我的问题,我在哪里可以将参数传递给这个函数?

我希望有人有这个图书馆的经验可以清除我的怀疑 .

谢谢 .

SOLUTION:

@Joan-Diego Rodriguez 在如何在注册过程中向tank_auth的user_profiles表中添加其他字段方面有很好的做法 .

CodeIgniter: Passing fields to user_profiles database with Tank_auth

希望这可以帮助那些寻找相同解决方案的人 .

1 回答

  • 0

    您好我已经遇到过这个问题,我的解决方案很简单

    create user - > create user_profiles - > update user_profiles

    请转到TA模型“用户”并添加此方法/功能

    function update_user_profile($user_id, $data)
    {
        $this->db->where('user_id', $user_id);
        $this->db->update($this->profile_table_name, $data);
    }
    

    制作auth.php的副本(以防万一)并在注册方法中做一些调整(如下)请注意 here is some more code 把原文放在那里(我修改了我的) .

    if ($this->form_validation->run()) {
    // validation ok
    
        if (!is_null(<here is some more code>)) {
    
            $user_data = array(
        'first_name' => $this->form_validation->set_value('first_name'),
        'last_name' => $this->form_validation->set_value('last_name'),
        );
    
        //$this->load->model('tank_auth/users');
        $this->users->update_user_profile($data['user_id'], $user_data); //update happens
    
        } else {
    
            $errors = $this->tank_auth->get_error_message();
            foreach ($errors as $k => $v)   $data['errors'][$k] = $this->lang->line($v);
    
        }
    }
    

    edit 获取用户需要使用SQL JOIN作为示例显示(带有额外的first_name / last_name)我希望你能得到这个想法

    检索所有用户将其放入用户模型中

    function get_all_users() {
        $this->db->select('users.id, users.username, users.activated, users.banned, users.ban_reason, user_profiles.id as up_id, user_profiles.first_name, user_profiles.last_name');
        $this->db->from('users');
        $this->db->join('user_profiles', 'users.id = user_profiles.user_id');
        return $this->db->get()->result();
    }
    

    检索一个用户(刚刚添加了where子句)

    function get_ser($id){
        $this->db->select('users.id, users.username, users.activated, users.banned, users.ban_reason, user_profiles.id as up_id, user_profiles.first_name, user_profiles.last_name');
        $this->db->from('users');
        $this->db->join('user_profiles', 'users.id = user_profiles.user_id');
        $this->db->where('users.id', $id);
        $q = $this->db->get();
        return ($q->num_rows() > 0) ? $q->result()[0] : FALSE;
    }
    

相关问题