【问题标题】:Codeigniter Tank_auth adding fieldsCodeigniter Tank_auth 添加字段
【发布时间】:2013-09-18 03:13:42
【问题描述】:

我使用 CI tank_auth 库有用户的注册表,需要添加 tank_auth 没有附带的新字段。

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

比方说,我想在注册期间添加额外的“Name”字段,我已经在视图和function register() 控制器中创建了它,这很好,除非我想把它放在@987654324 中@table,我创建了一个名为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,这是我的问题,我可以在哪里传递这个函数的参数?

希望有这个库经验的人能解开我的疑惑。

谢谢。

解决方案:

@Joan-Diego Rodriguez 在注册过程中如何将其他字段添加到 tank_auth 的 user_profiles 表中有一个非常好的做法。

CodeIgniter: Passing fields to user_profiles database with Tank_auth

希望这对寻找相同解决方案的人有所帮助。

【问题讨论】:

    标签: php codeigniter tankauth


    【解决方案1】:

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

    创建用户 -> 创建用户配置文件 -> 更新用户配置文件

    请转到 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(以防万一)并在注册方法中进行一些调整(如下)请注意这里还有一些代码将原始代码放在那里(我修改了我的)。

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

    编辑 要获取用户,您需要使用 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;
    }
    

    【讨论】:

    • 谢谢。也许您知道如何从user_profiles 表中检索用户信息?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多