CodeIgniter:使用 Tank_auth 将字段传递到 user_profiles 数据库

标签 codeigniter tankauth

我正在绞尽脑汁地思考一个可能很简单的问题。 MVC 和 codeigniter 相对较新。我使用 Tank_auth 进行用户注册,它附带一个数据库表 user_profiles,我对其进行了稍微修改以添加“名字”、“姓氏”、“家庭电话”等内容。

我已将适当的字段添加到我的 register_form.php View 中,并遵循此问题的建议:Tank Auth Adding Fields和其他人,试图更新所有必要的东西。不幸的是,虽然users 表已正确填充,但user_profiles 表却未正确填充。我已经用 firebug 仔细检查了 View 是否正确发布,但模型没有获取数据,并且我不断收到错误:

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

查看:

$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>

我在这方面已经工作了太久了,我希望有一双新鲜的(并且知识渊博的)眼睛能够看到我看不到的东西。

最佳答案

要记录在user_profile中的数据沿着以下链传递:

View -> Controller -> 库/tank_auth/create_user() -> 模型/用户/用户/create_user() -> 模型/create_profile

因此,您需要确保每次都传递该变量。 这是我的工作解决方案,基于您在问题中提到的修改:

查看+ Controller :

你的解决方案很好

图书馆

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

型号:

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

关于CodeIgniter:使用 Tank_auth 将字段传递到 user_profiles 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9908899/

相关文章:

javascript - 当鼠标移开时保持下拉菜单打开?

php - 如果多个记录在一个 ID 上,如何获取单个记录?

database - 代码点火器 TankAuth : How to move database to new server with phpass hash portable set to FALSE

php - 我怎样才能告诉 Tank Auth 完全不激活用户?

php - Tank Auth 登录在本地成功,在服务器上失败

php - 内联三元运算符不工作

php - codeigniter 3.1.0 中的 protect_identifiers 问题

php - CodeIgniter - 引用 URL - 保存在数据库中

php - 销毁 session 但保留闪存数据

php - Codeigniter 库中的短函数