2012-03-28 56 views
2

我對可能是一個簡單問題的大腦很感興趣。相對較新的MVC和codeigniter。我正在使用tank_auth進行用戶註冊,並且它附帶了一個db表user_profiles,我稍微修改了它以添加諸如「名字」,「姓氏」,「家庭電話」等等的內容。CodeIgniter:使用Tank_auth將字段傳遞給user_profiles數據庫

我添加了適當的字段到我的register_form.php視圖和以下建議從這個問題:Tank Auth Adding Fields和其他人,試圖更新所有必要的東西。不幸的是,儘管users表正確填充,但user_profiles表沒有。我用雙螢火蟲的觀點是否正確張貼檢查,但該模型不拾取數據,和我不斷收到錯誤:

A PHP Error was encountered 

Severity: Notice 

Message: Undefined variable: firstname 

Filename: tank_auth/users.php 

Line Number: 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> 

我一直在努力工作太久,希望一雙新鮮(而且知識淵博)的眼睛能看到我不能做的事。

回答

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

你需要通過$名字作爲參數傳遞給函數...

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

試過了,但還是得到了'Message:Und INSERT INTO'user_profiles'('user_id','firstname')VALUES(61,NULL) 文件名:/ home/snowmobi/public_html/davidcarter.net/demos/CI/models/tank_auth/users.php' – Davezatch 2012-03-29 07:50:31

2

要被記錄在user_profile沿folllowing鏈傳遞的數據:

視圖 - >控制器 - >庫/ tank_auth/create_user() - >模型/ users/users/create_user() - > model/create_profile

因此,您需要確保每次都傳遞該變量。 這是我工作的解決方案,對你的問題中提到的修改建立:

VIEW + CONTROLER:

你的解決方案有利於

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

我使用了這種方法,它適用於email_activation設置爲FALSE的情況。如果設置爲TRU,則從controller/auth/activate() - > library/tank_auth/activate() - > model/users/users/activate() - > model/create_profile調用model/create_profile。在這種情況下,我沒有數據傳遞到最後,因爲用戶可能以後離開了頁面並激活了帳戶。你有這種情況下的解決方案?我正在考慮一些臨時表來存儲配置文件信息,並在激活過期時刪除它 – 2016-12-01 11:14:38