2011-11-17 125 views
0

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

我的注册表格当前将所有注册数据写入数据库并创建并激活代码。

如何将变量'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); 
        } 

回答

1

可以只是返回成功值的数组,如果失败,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查找用户(假设它应该是唯一的)。

+0

我遵循你的建议,并运行第二个功能,通过电子邮件地址查找用户并获取激活码。谢谢 – hairynuggets

相关问题