CodeIgniter Forums
how to load a model with a parameter to construct the model.... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: how to load a model with a parameter to construct the model.... (/showthread.php?tid=15953)



how to load a model with a parameter to construct the model.... - El Forum - 02-19-2009

[eluser]Unknown[/eluser]
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?


how to load a model with a parameter to construct the model.... - El Forum - 02-19-2009

[eluser]darkhouse[/eluser]
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:

Code:
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;
   }

}



how to load a model with a parameter to construct the model.... - El Forum - 02-20-2009

[eluser]Unknown[/eluser]
a good idea thank you !