代码点火器Tank_auth添加字段


Codeigniter Tank_auth adding fields

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

确实参考了这个坦克身份验证添加字段,但它从未帮助我解决我的疑问。

假设,我想在注册过程中有额外的"名称"字段,我已经在视图和function register()控制器中创建了它,这工作正常,除了我想把它放在user_profiles表中时,我创建了一个名为 name 的列,然后在tank_auth模型的用户中.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:使用Tank_auth将字段传递到user_profiles数据库

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

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

创建用户 ->创建user_profiles ->更新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);
}

复制身份验证.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);
    }
}

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