CodeIgniter: Passing fields to user_profiles database with Tank_auth

折月煮酒 提交于 2019-11-30 20:49:25

问题


I'm wracking my brain on what is probably a simple issue. Relatively new to MVC and codeigniter. I'm using tank_auth for user registration, and it comes with a db table user_profiles which I've altered slightly to add things like 'firstname', 'lastname', 'homephone', etc.

I've added the appropriate fields to my register_form.php view and following advice from this question: Tank Auth Adding Fields and others, tried to update all necessary stuff. Unfortunately, while the users table gets populated properly, the user_profiles table does not. I've double checked with firebug that the view is posting properly, but the model is not picking up the data, and I keep getting the error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: firstname

Filename: tank_auth/users.php

Line Number: 382

Using var_dump, I can see that the controller function is not receiving 'firstname' or anything else and they are NULL, but the data going into users is being sent properly.

Here's the relevant code:

Model:

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

Controller:

function register()
{
    if ($this->tank_auth->is_logged_in()) {                                 // logged in
        redirect('');

    } elseif ($this->tank_auth->is_logged_in(FALSE)) {                      // logged in, not activated
        redirect('/auth/send_again/');

    } elseif (!$this->config->item('allow_registration', 'tank_auth')) {    // registration is off
        $this->_show_message($this->lang->line('auth_message_registration_disabled'));

    } else {
        $use_username = $this->config->item('use_username', 'tank_auth');
        if ($use_username) {
            $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->config->item('username_min_length', 'tank_auth').']|max_length['.$this->config->item('username_max_length', 'tank_auth').']|alpha_dash');
        }
        $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
        $this->form_validation->set_rules('firstname', 'Firstname', 'trim|xss_clean');
        $this->form_validation->set_rules('lastname', 'Lastname', 'trim|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length['.$this->config->item('password_min_length', 'tank_auth').']|max_length['.$this->config->item('password_max_length', 'tank_auth').']|alpha_dash');
        $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean|matches[password]');

        $captcha_registration   = $this->config->item('captcha_registration', 'tank_auth');
        $use_recaptcha          = $this->config->item('use_recaptcha', 'tank_auth');
        if ($captcha_registration) {
            if ($use_recaptcha) {
                $this->form_validation->set_rules('recaptcha_response_field', 'Confirmation Code', 'trim|xss_clean|required|callback__check_recaptcha');
            } else {
                $this->form_validation->set_rules('captcha', 'Confirmation Code', 'trim|xss_clean|required|callback__check_captcha');
            }
        }
        $data['errors'] = array();

        $email_activation = $this->config->item('email_activation', 'tank_auth');

        if ($this->form_validation->run()) {                                // validation ok
            if (!is_null($data = $this->tank_auth->create_user(
                    $use_username ? $this->form_validation->set_value('username') : '',
                    $this->form_validation->set_value('email'),
                    $this->form_validation->set_value('password'),
                    $this->form_validation->set_value('firstname'),
                    $this->form_validation->set_value('lastname'),
                    $this->form_validation->set_value('homephone'),
                    $this->form_validation->set_value('cellphone'),
                    $email_activation))) {                                  // success

                $data['site_name'] = $this->config->item('website_name', 'tank_auth');

                if ($email_activation) {                                    // send "activate" email
                    $data['activation_period'] = $this->config->item('email_activation_expire', 'tank_auth') / 3600;

                    $this->_send_email('activate', $data['email'], $data);

                    unset($data['password']); // Clear password (just for any case)

                    $this->_show_message($this->lang->line('auth_message_registration_completed_1'));

                } else {
                    if ($this->config->item('email_account_details', 'tank_auth')) {    // send "welcome" email

                        $this->_send_email('welcome', $data['email'], $data);
                    }
                    unset($data['password']); // Clear password (just for any case)

                    $this->_show_message($this->lang->line('auth_message_registration_completed_2').' '.anchor('/auth/login/', 'Login'));
                }
            } else {
                $errors = $this->tank_auth->get_error_message();
                foreach ($errors as $k => $v)   $data['errors'][$k] = $this->lang->line($v);
            }
        }
        if ($captcha_registration) {
            if ($use_recaptcha) {
                $data['recaptcha_html'] = $this->_create_recaptcha();
            } else {
                $data['captcha_html'] = $this->_create_captcha();
            }
        }
        $data['use_username'] = $use_username;
        $data['captcha_registration'] = $captcha_registration;
        $data['use_recaptcha'] = $use_recaptcha;
        $this->load->view('auth/register_form', $data);
    }
}

View:

$firstname = array(
'name'  => 'firstname',
'id'    => 'firstname',
'value' => set_value('firstname'),
'maxlength' => 40,
'size'  => 30,
);

...

<tr>
    <td><?php echo form_label('First Name', $firstname['id']); ?></td>
    <td><?php echo form_input($firstname); ?></td>
    <td style="color: red;"><?php echo form_error($firstname['name']); ?><?php echo isset($errors[$firstname['name']])?$errors[$firstname['name']]:''; ?></td>
</tr>

I have been working on this far too long, am hoping that a fresh (and knowledgeable) pair of eyes can see what I cannot.


回答1:


The data to be recorded in user_profile is passed along the folllowing chain:

view -> controller -> library/tank_auth/create_user() -> model/users/users/create_user() -> model/create_profile

Therefore you need to make sure that the variable is passed along every time. Here is my working solution, building up on the modifications that you mentioned in the question:

VIEW + CONTROLER:

Your solution is good

LIBRARY

function create_user($username, $email, $password, $firstname, $lastname, $company='', $email_activation)
{
    if ((strlen($username) > 0) AND !$this->ci->users->is_username_available($username)) {
        $this->error = array('username' => 'auth_username_in_use');

    } elseif (!$this->ci->users->is_email_available($email)) {
        $this->error = array('email' => 'auth_email_in_use');

    } else {
        // Hash password using phpass
        $hasher = new PasswordHash(
                $this->ci->config->item('phpass_hash_strength', 'tank_auth'),
                $this->ci->config->item('phpass_hash_portable', 'tank_auth'));
        $hashed_password = $hasher->HashPassword($password);

        $data = array(
            'username'  => $username,
            'password'  => $hashed_password,
            'email'     => $email,
            'last_ip'   => $this->ci->input->ip_address(),
        );

        $data_profile = array(
            'firstname' => $firstname,
            'lastname' => $lastname,
            'company' => $company,

        );


        if ($email_activation) {
            $data['new_email_key'] = md5(rand().microtime());
        }
        if (!is_null($res = $this->ci->users->create_user($data, $data_profile, !$email_activation))) {
            $data['user_id'] = $res['user_id'];
            $data['password'] = $password;
            unset($data['last_ip']);
            return $data;
        }
    }
    return NULL;
}

MODEL:

function create_user($data, $data_profile, $activated = TRUE)
{
    $data['created'] = date('Y-m-d H:i:s');
    $data['activated'] = $activated ? 1 : 0;

    var_dump($data);

    if ($this->AuthDb->insert($this->table_name, $data)) {
        $user_id = $this->AuthDb->insert_id();
        $this->create_profile($user_id, $data_profile);
        return array('user_id' => $user_id);
    }
    return NULL;
}

[...]

private function create_profile($user_id, $data_profile)
{
    $this->AuthDb->set('user_id',   $user_id);
    $this->AuthDb->set('firstname', $data_profile['firstname']);
    $this->AuthDb->set('lastname',  $data_profile['lastname']);
    $this->AuthDb->set('company',   $data_profile['company']);
    return $this->AuthDb->insert($this->profile_table_name);
}



回答2:


You need to pass $firstname as a parameter to the function...

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



回答3:


Ok so I figured it out, in case anyone googles this answer down the line. I'm not sure if this is the 'proper' or most elegant solution, but does fine for my purposes. I edited the create_profile function like so:

private function create_profile($user_id)
{
    $this->db->set('user_id', $user_id);
    $data = array(
        'firstname' => $this->input->post('firstname'),
        );
    $this->db->insert($this->profile_table_name, $data);
}


来源:https://stackoverflow.com/questions/9908899/codeigniter-passing-fields-to-user-profiles-database-with-tank-auth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!