我一直在努力解决这个问题很长一段时间,我仍然不知道如何让这个工作。
目前我需要在Model到Controller中显示我新生成的用户名。但我似乎无法称呼它。以下是我的代码:
模型formval_callbacks.php:
public function _username_check( $user_name )
{
$this->db->select('user_name');
$this->db->like('user_name', $user_name);
$this->db->order_by('user_date', 'desc');
$this->db->limit(1);
$query = $this->db->get('users');
if ($query->num_rows() > 0)
{
$row = $query->row();
$db_username = $row->user_name;
$username_counter = preg_match("/".$user_name."(\d+)/", $db_username, $matches) ? (int)$matches[1] : NULL;
$username_counter++;
return $user_name.$username_counter;
}else{
$username_counter = 1;
return $user_name.$username_counter;
}
}
模型user_model.php:
public function create_user( $role, $insert_array = array() )
{
// The form validation class doesn't allow for multiple config files, so we do it the old fashion way
$this->config->load( 'form_validation/administration/create_user/create_' . $role );
$this->validation_rules = config_item( $role . '_creation_rules' );
// If the data is already validated, there's no reason to do it again
if( ! empty( $insert_array ) OR $this->validate() === TRUE )
{
// Prepare user_data array for insert into user table
$user_data = array(
'user_name' => ( isset( $insert_array['user_name'] ) ) ? $insert_array['user_name'] : set_value('user_name'),
'user_pass' => ( isset( $insert_array['user_pass'] ) ) ? $insert_array['user_pass'] : set_value('user_pass'),
'user_email' => ( isset( $insert_array['user_email'] ) ) ? $insert_array['user_email'] : set_value('user_email')
);
// User level derived directly from the role argument
$user_data['user_level'] = $this->authentication->levels[$role];
// If we are using form validation for the user creation
if( empty( $insert_array ) )
{
// Remove some user_data elements from _field_data array as prep for insert into profile table
$this->form_validation->unset_field_data( array(
'user_name',
'user_pass',
'user_email'
));
// Create array of profile data
foreach( $this->form_validation->get_field_data() as $k => $v )
{
$profile_data[$k] = $v['postdata'];
}
// Unset all data for set_value(), so we can create another user
$this->kill_set_value();
}
// If we are not using form validation for the user creation
else
{
// Remove some insert_array elements as prep for insert into profile table
unset( $insert_array['user_name'] );
unset( $insert_array['user_pass'] );
unset( $insert_array['user_email'] );
// Profile data is insert array
$profile_data = $insert_array;
}
// Encrypt any sensitive data
if( isset( $profile_data['license_number'] ) )
{
$this->load->library('encrypt');
$profile_data['license_number'] = $this->encrypt->encode( $profile_data['license_number'] );
}
// Create a random user id if not already set
$random_unique_int = $this->get_unused_id();
// Generate random user salt
$user_salt = $this->authentication->random_salt();
// Perform transaction
$this->db->trans_start();
$user_data['user_id'] = $random_unique_int;
$user_data['user_pass'] = $this->authentication->hash_passwd( $user_data['user_pass'], $user_salt );
$user_data['user_salt'] = $user_salt;
$user_data['user_date'] = time();
$user_data['user_modified'] = time();
// Insert data in user table
$this->db->set($user_data)
->insert( config_item('user_table'));
$profile_data['user_id'] = $random_unique_int;
// Insert data in profile table
$this->db->set($profile_data)
->insert( config_item( $role . '_profiles_table'));
// Complete transaction
$this->db->trans_complete();
// Verify transaction was successful
if( $this->db->trans_status() !== FALSE )
{
// Load var to confirm user inserted into database
$this->load->vars( array( 'user_created' => 1 ) );
}
return TRUE;
}
return FALSE;
}
Controller register.php:
public function index()
{
// Load resources
$this->load->library('csrf');
$this->load->model('registration_model');
$reg_mode = $this->registration_model->get_reg_mode();
// Check to see if there was a registration submission
if( $this->csrf->token_match )
{
// If mode #1, registration allows for instant user creation without verification or approval.
if( $reg_mode == 1 )
{
$_POST['user_level'] = 1;
$this->load->model('user_model');
$this->user_model->create_user( 'customer', array() );
$this->load->model('formval_callbacks');
$view_data['new_username'] = $this->formval_callbacks->_username_check();
}
}
// If for some reason the user is already logged in
$this->is_logged_in();
// Send registration mode to view
$view_data['reg_mode'] = $reg_mode;
// Ouput alert-bar message if cookies not enabled
$this->check_cookies_enabled();
$data = array(
'title' => WEBSITE_NAME . ' - Account Registration',
'content' => $this->load->view( 'public/register/registration_form', $view_data, TRUE ),
// Load the show password script
'javascripts' => array(
'js/jquery.passwordToggle-1.1.js',
'js/jquery.char-limiter-3.0.0.js',
'js/default-char-limiters.js'
),
// Use the show password script
'extra_head' => '
<script>
$(document).ready(function(){
$("#show-password").passwordToggle({target:"#user_pass"});
});
</script>
'
);
$this->load->view( $this->template, $data );
}
此行有错误,因为我从未输入任何参数:
$view_data['new_username'] = $this->formval_callbacks->_username_check();
如果我只想获取新生成的用户名而不输入参数,该怎么办?
此外,在此函数中从formval_callback.php或user_model.php获取新生成的用户名是否更好:
if( $this->db->trans_status() !== FALSE )
希望你们能在这里帮助我。