2013-07-20 54 views
1

我有一些課程,我要加載的模式,但問題是,我想他們在幾種模式分開,現在我有一個文件處理類,看起來像這樣:從另一個模型CodeIgniter調用模型函數?

class email { 
     function add($email, $name, $quiet=NULL, $actiovation=NULL) { 
     global $secretstring; 
     global $mail; 
     global $path; 
     global $activating; 

     if (strlen($email) < 1) { 
      if (!isset($quiet)) { 
       msg::getInstance()->addSuccess("Please enter your email address."); 
      } 
      $error = 1; 
     } 
     if (strlen($name) < 1) { 
      if (!isset($quiet)) { 
       msg::getInstance()->addSuccess("Please enter your name."); 
      } 
      $error = 1; 
     } 
        $addData = mysql_fetch_array(sql::getInstance()->query("SELECT id FROM emails WHERE email='".sql::getInstance()->sec($stamp)."'")); // getting id of this email 

} 

class msg { 
    static private $instance = NULL; 
    function addSuccess($success) { 
     $this->success .= $success."\\n "; 
    } 
} 

class sql { 
    static private $instance = NULL; 

    function query($query) { 
     return mysql_query($query); 
    } 

    function sec($string) { 
     return mysql_real_escape_string(htmlspecialchars($string)); 
} 

隨着比如我可以輕鬆地從另一個類調用函數?但問題是,當我想從另一個模型調用函數時,在一個模型中,我不知道如何在CI中創建該函數?任何幫助我都做了一個簡單的例子來說明我是如何製作課程的。

回答

4

你只需要確保您加載其中,你想調用函數要在使用這些功能的模型模型。就像你將在控制器裏做。

事情是這樣的:

public class Email extends CI_Model{ 
    function add(...){ 
     $this->load->model('msg'); 
     $this->msg->addSuccess(...); 
    } 
} 

它真的那麼容易,因爲這一點。

0

CI有兩個方法來調用函數。無論您創建哪種模型,都必須在該類中加載該模型,以便使用該類。

  • 手動加載。

    $這 - >負載>模型( 「your_model_name」);

注意:請在該類的上方呼叫您要使用的行。

  • 自動加載

裝入類放置在應用程序/配置/ auto_load.php

呼叫的功能,則auto_load類:

$這個 - > your_class_name->函數名(參數);

閱讀這些資源,更多的幫助和澄清。

  1. https://ellislab.com/codeigniter/user-guide/libraries/loader.html
  2. https://ellislab.com/codeigniter/user-guide/general/autoloader.html
相關問題