2

我正在为一个可能很简单的问题绞尽脑汁。MVC 和 codeigniter 相对较新。我正在使用 tank_auth 进行用户注册,它带有一个 db 表user_profiles,我对它稍作修改,添加了“firstname”、“lastname”、“homephone”等内容。

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

遇到 PHP 错误

严重性:通知

消息:未定义的变量:名字

文件名:tank_auth/users.php

行号:382

使用 var_dump,我可以看到控制器函数没有接收到“名字”或其他任何内容,它们为 NULL,但进入的数据users正在正确发送。

以下是相关代码:

模型:

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

控制器:

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>

我一直在研究这个太久了,我希望一双新鲜的(和知识渊博的)眼睛能看到我看不到的东西。

4

3 回答 3

2

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

视图 -> 控制器 -> 库/tank_auth/create_user() -> 模型/用户/用户/create_user() -> 模型/create_profile

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

查看 + 控制器:

你的解决方案很好

图书馆

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);
}
于 2013-07-25T20:20:46.337 回答
0

您需要将 $firstname 作为参数传递给函数...

   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);
    }
于 2012-03-28T21:10:00.727 回答
0

好的,所以我想通了,以防万一有人用谷歌搜索这个答案。我不确定这是否是“正确”或最优雅的解决方案,但对于我的目的来说很好。我像这样编辑了create_profile函数:

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);
}
于 2012-03-30T09:36:20.907 回答