0

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.

I did refer to this Tank Auth Adding Fields, but it never helps me to solve my doubt.

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:

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?

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.

CodeIgniter: Passing fields to user_profiles database with Tank_auth

Hope this would help someone who looking for the same solution.

Community
  • 1
  • 1
conmen
  • 2,377
  • 18
  • 68
  • 98

1 Answers1

0

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

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).

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

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)

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;
}
Kyslik
  • 8,217
  • 5
  • 54
  • 87
  • Thanks. Maybe you could have any idea of how to retrieve user's info from `user_profiles` table? – conmen Sep 18 '13 at 10:05