【问题标题】:Calling Model Functions from a Library从库中调用模型函数
【发布时间】:2010-03-28 15:11:58
【问题描述】:

我已经将一个普通的 PHP 类变成了一个库,所以我可以在 Codeigniter 中将它用作一个库。我可以加载它并在该类中调用我需要的函数。 Here is that class to help with the question

但是,有很多地方我必须在课堂上调用函数。这些函数驻留在实例化我的类的模型中。我该如何做到这一点,因为目前正常的通话不起作用。这是我的代码:


class Controlpanel_model extends Model {

    var $category = '';
    var $dataa = 'a';

    function Controlpanel_model(){      

        parent::Model();

    }

    function import_browser_bookmarks(){

        $this->load->library('BookmarkParser');
        /*
        *In this function call to the class I pass 
        * what model functions exist that it should call
        * You can view how this done by clicking the link above and looking at line 383
        */
        $this->bookmarkparser->parseNetscape("./bookmarks.html", 0, 'myURL', 'myFolder'); 
        return $this->dataa;

    }

    function myURL($data, $depth, $no) { 

        $category = $this->category;
        $this->dataa .= 'Tag = '.$category.'<br />'.'URL = '.$data["url"].'<br />'.'Title = '.$data["descr"].'<br />'.'<br /><br />';
    } 

    function myFolder($data, $depth, $no) {

        $this->category = $data["name"];

    }   

}

感谢大家的帮助。

【问题讨论】:

    标签: php model-view-controller codeigniter class


    【解决方案1】:

    澄清一下,您在调用传递给您的书签解析器的模型函数时遇到问题?

    在您的库中,您需要通过以下方式引用模型本身及其功能:

    // Based on the signature you provided
    parseNetscape($url, $folderID, $urlFunction, $folderFunction) {
       get_instance()->Controlpanel_model->$urlFunction();
       get_instance()->Controlpanel_model->$folderFunction();
    }
    

    我们需要使用 get_instance(),因为库不会继承所有 CI 好东西。 这将假设您的模型已经加载。我不确定您在引用 $CI 实例或动态调用函数时遇到了什么问题。

    希望这就是您想要的。

    【讨论】:

    • 完美——正是我想要的!
    猜你喜欢
    • 2012-05-12
    • 2015-05-13
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2016-05-27
    相关资源
    最近更新 更多