dongzhang1987
2012-12-03 23:29
浏览 30
已采纳

如何从模型中提取变量以在CodeIgniter中的控制器上使用?

I am trying to use a variable in my controller that has been created in my model.

Here is the model:

public function validate_retrieve($data) {

            $query = $this->db->where($data)->get('users', '1');

            foreach ($query->result() as $user)
            {
                $user->email;
                $user->salt;
                $user->id;

            }

            $token = sha1($user->email.$user->salt).dechex($user->id);
            $reset_token = array(
                'token' => $token,
                'email' => $user->email
            );

            $insert = $this->db->insert('reset', $reset_token, '1');
            return $reset_token;
        }

and on my controller I'm trying to use $token that was created in the model:

public function retrieve()
        // REQUEST PASSWORD RESET
        // LOADED WHEN THE FORM IS SUBMITTED OFF THE PASSWORD PAGE AND SENDS THE EMAIL WITH TOKEN AND INSTRUCTIONS
        {
            $this->load->library('form_validation');
            $this->load->library('session');
            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
            $this->load->model('user_model', 'um');
            $this->load->library('encrypt');
            $this->load->helper('url');
            $submit = $this->input->post('submit');
            $salt = $this->_salt();

            if($submit)
            // IF THE SUBMIT BUTTON IS SET
            {

                // START PROCESS TO CREATE $USER VARIABLE THAT HOLDS WHAT THE USER ENTERED IN THE FORM AND THAT CAN GET CHECKED AGAINST THE DB IN THE MODEL
                $user = $this->um->validate_retrieve(array('email' => $this->input->post('email')));


                // IF THE USER IS CREATED AND CHECKS OUT AND ALL OF THE ERRORS ARE CLEARED ON THE FORM
                if( $user && $this->form_validation->run() == TRUE ) {

                    $domain = "clci.dev/index.php";

                    // CREATE A TOKEN LINK TO SEND TO THE USERS EMAIL THAT EXIST IN THE DB AND WAS ENTERED

                    $token = $this->um->token;
                    $link = "http://www.".$domain."/auth/reset/?token=$token";


                        $this->load->library('email');

                        $this->email->from('noreply@cysticlife.org', 'CysticLife');
                        $this->email->to($this->input->post('email')); 

                        $this->email->subject('Reset Password');
                        $this->email->message("Please go to the following web address to reset your password:

$link

-Your friends at CysticLife

Please remember to add the cysticlife.org domain to your address book to ensure that you receive your CysticLife e-Notifications as requested.");  

                        $this->email->send();
                        redirect('auth/success');
                        exit;

                    }
                    $this->form_validation->run() == FALSE;
                    $data['main_content'] = 'auth/password';
                    $this->load->view('includes/templates/main_page_template', $data);
                    $data['email_error'] = 'This email is invalid';
                }
            }

thanks in advance

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshuzhuo5659 2012-12-03 23:33
    已采纳

    You're trying to access an array as an object.

    $reset_token = $this->um->validate_retrieve($whatever_id);
    $token = $reset_token['token'];
    

    That's how you access the token portion of the returned results from this function.

    And according to your latest update, you would actually access it like this:

    $token = $user['token'];
    

    Because in the following line you assign the results of the validate_retrieve method to the variable $user:

    $user = $this->um->validate_retrieve(array('email' => $this->input->post('email')));
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题