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

时间:2012-12-03 23:29:46

标签: php oop codeigniter

我正在尝试在我的模型中创建的控制器中使用变量。

以下是模型:

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

在我的控制器上,我正在尝试使用模型中创建的$token

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:\n\n$link\n\n-Your friends at CysticLife\n\nPlease 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';
                }
            }

提前致谢

1 个答案:

答案 0 :(得分:2)

您正在尝试将数组作为对象访问。

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

这就是你如何从这个函数访问返回结果的标记部分。

根据您的最新更新,您实际上会像这样访问它:

$token = $user['token'];

因为在以下行中您将validate_retrieve方法的结果分配给变量$ user:

$user = $this->um->validate_retrieve(array('email' => $this->input->post('email')));