塑料做的铁钉 发表于 2009-2-19 16:29:54

装置模型时如何让模型类带参数?

比如说:

class M_model extends Model {


function __construct($user_id = NULL) {
parent::Model ();
//do something here...
}

装置这个模型时,如何传递$user_id 参数过来构造函数。。。。

。。。。我说得够明白了咩?

Hex 发表于 2009-2-19 23:10:54

根据手册来说,是不支持这样。

塑料做的铁钉 发表于 2009-2-20 18:37:04

本帖最后由 塑料做的铁钉 于 2009-2-20 19:04 编辑

how to load a model with a parameter to construct the model….

just like :

class M_model extends Model {


function __construct($user_id = NULL) {
parent::Model ();
//do something here…
}

when i want
$this->load->model(‘m_model’);

how to transfer the parameter $user_id to the model?

---------------------------------------------------------------------------------------

That would require changing the loader library I believe, as it’s the class that would say
$this->m_model = new M_model();

But instead of loading it in the constructor, why can’t you just setup an init method, like this:

class M_model extends Model {

   private $user_id = NULL;

   public function __construct() {
      parent::Model ();
      //do something here…
   }

   public function init($user_id = NULL){
      if($user_id !== NULL) $this->user_id = $user_id;
   }

}

解决方案放上来分享

Hex 发表于 2009-2-20 22:30:24

强!!学习了 !!
页: [1]
查看完整版本: 装置模型时如何让模型类带参数?