Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从另一个模型CodeIgniter调用模型函数?_Codeigniter_Model_Instance - Fatal编程技术网

从另一个模型CodeIgniter调用模型函数?

从另一个模型CodeIgniter调用模型函数?,codeigniter,model,instance,Codeigniter,Model,Instance,我有一些类,我想作为一个模型加载,但问题是我想将它们分成几个模型,现在我有一个文件,其中的类如下所示: class email { function add($email, $name, $quiet=NULL, $actiovation=NULL) { global $secretstring; global $mail; global $path; global $activating; if

我有一些类,我想作为一个模型加载,但问题是我想将它们分成几个模型,现在我有一个文件,其中的类如下所示:

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));
}
public class Email extends CI_Model{
    function add(...){
        $this->load->model('msg');
        $this->msg->addSuccess(...);
    }
}
class电子邮件{
函数添加($email、$name、$quiet=NULL、$actiovation=NULL){
全球$secretstring;
全球美元邮件;
全球$path;
全球美元激活;
如果(strlen($email)<1){
如果(!isset($quiet)){
msg::getInstance()->addSuccess(“请输入您的电子邮件地址”);
}
$error=1;
}
if(strlen($name)<1){
如果(!isset($quiet)){
msg::getInstance()->addSuccess(“请输入您的姓名”);
}
$error=1;
}
$addData=mysql_fetch_数组(sql::getInstance()->query(“从电子邮件中选择id=”)。sql::getInstance()->sec($stamp)。”;//获取此电子邮件的id
}
类味精{
静态私有$instance=NULL;
函数addSuccess($success){
$this->success.=$success.“\\n”;
}
}
类sql{
静态私有$instance=NULL;
函数查询($query){
返回mysql\u查询($query);
}   
函数秒($string){
返回mysql_real_escape_字符串(htmlspecialchars($string));
}

有了实例,我可以很容易地从另一个类调用函数?但问题是,当我想从另一个模型调用函数时,在一个模型中,我不知道如何在CI中实现它?有什么帮助吗?我已经制作了一个简单的示例来说明我是如何创建类的。

您只需要确保在需要的模型中加载要调用函数的模型您希望在中使用这些函数,就像在控制器中一样

大概是这样的:

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));
}
public class Email extends CI_Model{
    function add(...){
        $this->load->model('msg');
        $this->msg->addSuccess(...);
    }
}

这真的很简单。

您只需要确保在要使用这些函数的模型中加载要调用函数的模型。就像在控制器中一样

大概是这样的:

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));
}
public class Email extends CI_Model{
    function add(...){
        $this->load->model('msg');
        $this->msg->addSuccess(...);
    }
}

其实很简单。

CI有两种方法来调用函数。无论创建什么模型,都必须将该模型加载到要使用该类的类中

  • 手动加载

    $this->load->model(“您的模型名称”)

注意:在您想要使用的那个类中调用上面的行

  • 自动加载
在application/config/auto_Load.php中的auto_Load类中加载类

函数调用:

$this->your_class_name->function_name(参数)

阅读这些参考资料以获得更多帮助和说明


  • CI有两种方法来调用函数。无论创建什么模型,都必须将该模型加载到要使用该类的类中

    • 手动加载

      $this->load->model(“您的模型名称”)

    注意:在您想要使用的那个类中调用上面的行

    • 自动加载
    在application/config/auto_Load.php中的auto_Load类中加载类

    函数调用:

    $this->your_class_name->function_name(参数)

    阅读这些参考资料以获得更多帮助和说明