简体   繁体   English

Codeigniter Tank_auth添加字段

[英]Codeigniter Tank_auth adding fields

I use CI tank_auth library to have user's registration form and need to add new fields that tank_auth didn't come with. 我使用CI tank_auth库获取用户的注册表格,并且需要添加tank_auth随附的新字段。

I did refer to this Tank Auth Adding Fields , but it never helps me to solve my doubt. 我确实提到了此Tank Auth添加字段 ,但是它永远无法帮助我解决我的疑问。

Let's say, I want to have additional ' Name ' field during registration, I had created it in views and function register() controller, that's works fine except when I want to put it in user_profiles table, I created a column called name , and then in tank_auth model's users.php, I find below method and added: 假设,我想在注册过程中有一个额外的“ 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);
}

When I run registration, no error occurred, I check with the table, the name field is not insert, so I guess it might be no parameter pass to $name , here is my problem, where could I pass the parameter to this function? 当我运行注册时,没有错误发生,我检查了表,没有插入name字段,所以我猜可能是没有参数传递给$name ,这是我的问题,我可以在哪里将参数传递给此函数?

I hope there is someone with experiences with this library can clear my doubt. 我希望有人对此库有经验,可以消除我的疑问。

Thanks. 谢谢。

SOLUTION: 解:

@Joan-Diego Rodriguez has a very good practice on how to add additional fields into tank_auth's user_profiles table during register process. @ Joan-Diego Rodriguez在如何在注册过程中将其他字段添加到tank_auth的user_profiles表中方面具有很好的实践。

CodeIgniter: Passing fields to user_profiles database with Tank_auth CodeIgniter:使用Tank_auth将字段传递到user_profiles数据库

Hope this would help someone who looking for the same solution. 希望这对寻求相同解决方案的人有所帮助。

Hello I came across this problem already, my solution is simple 您好我已经遇到了这个问题,我的解决方法很简单

create user -> create user_profiles -> update user_profiles 创建用户->创建用户个人资料->更新用户个人资料

please go to TA model "users" and add this method/function 请转到TA模型“用户”并添加此方法/功能

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

make copy of auth.php (just in case) and in register method do some adjustments (as follows) please note here is some more code put original in there (I modified mine). 制作auth.php的副本(以防万一),并在register方法中进行一些调整(如下所示),请注意, 这里还有一些原始代码 (我修改了我的代码)。

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 to get user you need to use SQLs JOIN as example shows (with extra first_name/last_name) I hope you will get the idea 编辑以获取用户,您需要使用SQLs JOIN作为示例显示(带有额外的first_name / last_name),希望您能理解

retrieve all users put this in users model 检索所有将其放入用户模型的用户

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

retrieve one user (just added where clause) 检索一个用户(仅添加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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM