CodeIgniter:使用Tank\u auth将字段传递到用户配置文件数据库

CodeIgniter:使用Tank\u auth将字段传递到用户配置文件数据库,codeigniter,tankauth,Codeigniter,Tankauth,我绞尽脑汁想一个可能很简单的问题。MVC和codeigniter相对较新。我使用tank_auth进行用户注册,它附带了一个db表user_profiles,我对它做了一些修改,添加了“firstname”、“lastname”、“homephone”等内容 我已经在register_form.php视图中添加了相应的字段,并遵循了这个问题的建议:和其他人,尝试更新所有必要的内容。不幸的是,虽然users表填充正确,但是user\u profiles表没有。我已与firebug再次检查视图是否正

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

我已经在register_form.php视图中添加了相应的字段,并遵循了这个问题的建议:和其他人,尝试更新所有必要的内容。不幸的是,虽然
users
表填充正确,但是
user\u profiles
表没有。我已与firebug再次检查视图是否正确发布,但模型没有拾取数据,并且我不断收到错误:

A PHP Error was encountered Severity: Notice Message: Undefined variable: firstname Filename: tank_auth/users.php Line Number: 382 控制器:

视图:

$firstname=array(
'name'=>'firstname',
'id'=>'firstname',
'value'=>set_value('firstname'),
“maxlength”=>40,
“大小”=>30,
);
...

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

您需要将$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);
    }

好吧,我想出来了,以防有人用谷歌搜索这个答案。我不确定这是否是“合适的”或最优雅的解决方案,但就我的目的而言,这很好。我编辑了
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);
}

要记录在用户_配置文件中的数据沿以下链传递:

视图->控制器->库/罐\u身份验证/创建\u用户()->模型/用户/用户/创建\u用户()->模型/创建\u配置文件

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

视图+控制器:

你的解决方案很好

图书馆

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

尝试了该操作,但仍然收到
消息:未定义变量:firstname
错误,以及以下信息:
列“firstname”不能为null插入到
user\u profiles`(
user\u id
firstname
)值(61,null)Filename:/home/snowmobi/public_html/davidcarter.net/demos/CI/models/tank_auth/users.php`我使用了这个方法,如果电子邮件激活设置为FALSE,它就会工作。如果设置为TRU,则从controller/auth/activate()->library/tank\u auth/activate()->model/users/users/activate()->model/create\u profile调用model/create\u 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);
}
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);
}