Codeigniter返回从模型到控制器的变量


Codeigniter return variables from model to controller

我正在为我的注册过程创建一个激活电子邮件。我需要将包含激活码和电子邮件地址的变量从模型传回控制器,在那里我将发送电子邮件。

我的注册表单目前写入所有注册数据到数据库,并创建和激活代码。

你怎么能传递变量'activation_code'和'email'从我的模型回到我的控制器在我的电子邮件中使用。

模型:

... 
        $new_member_insert_data = array(
            'first_name' => $first_name,
            'last_name' => $last_name,
            'email_address' => $email,
            'password' => hashPassword($salt, $password, $hash),
            'activation_code' => activationCode($email, $hash.$salt),
            'hash' => $hash
        );
        $insert = $this->db->insert('members', $new_member_insert_data);
        return $insert;
    }
控制器

  $this->load->model('members_model');
                  if($this->members_model->create_member())//return variables here somehow
                    {
//get activation code + email variables
//send activation email
                        $this->session->set_flashdata('success', 'success');
                        redirect('home', 'location');
                    }
                    else
                    {
                        $viewdata['main_content'] = $category;
                        $this->load->view('includes/template', $viewdata);
                    }

可以简单地在成功时返回值数组,或者在失败时返回FALSE:

$insert = $this->db->insert('members', $new_member_insert_data);
if ($insert) {
    return $new_member_insert_data; // Same data
}
else {
    return FALSE;
}

…但这实际上没有意义,因为您必须一些传递给模型的方法,以使其有用(或者预先访问其中的一些值,您给定的代码是不完整的)。此外,返回不同的数据类型可能会令人困惑,这通常是一个坏主意。

尽管没有访问您的完整代码在这里,我认为最安全,最准确的方式可能是在INSERT运行后运行另一个查询。例如,通过email_address查找用户(假设它是唯一的)。