Codeigniter共享变量

时间:2016-04-02 16:57:14

标签: php codeigniter scope

我有一个可能很愚蠢的问题 - 但这是由于我缺乏知识。我想知道我是否可以完成以下结果。

控制器

class Building extends CI_Controller {

 function __construct(){    
    parent::__construct();
    $this->load->model('buildings/buildings_model' , 'buildings');
    $is_locked = $this->buildings->_lock;
}

    // generic building overview. Includes data like name and location
    public function index($obj)
    {

        // the $obj param is the segment id from router
    }

    public function details($obj)
    {
        // here I would get some more specific data
    }

    public function associates($obj)
    {
       // list of things that the object is associated
    }

}

模型

class buildings_model extends MY_Model{
     public $_lock = false;
     function __construct() {   
        parent::__construct();
        // example pseudo code
        // db query select lock from obj where id = 1 for example
        // if lock == 1 { $this->_lock = TRUE; }
     }
}

现在问题..我分配了一个全局变量public $_lock = false;model中,因此可以在controller的每个范围内访问它。 但每次我访问views时,model constructor每次都会进行查询以检查对象是否被锁定。通常,如果建筑物被锁定,我需要一个全局标志 - 但不是每次页面视图都生成一次。

0 个答案:

没有答案
相关问题