EllisLab text mark
Advanced Search
     
how to pass argument to model constructor while loading a model
Posted: 27 May 2009 07:34 AM
Avatar
Joined: 2008-07-28
27 posts

how to pass argument to model constructor while loading a model normally we follow

$this->load-model('user'); 

  but I want to send parameters to this models constructor. Please explain me how to do this.

 Signature 

“God is Real unless declared Integersmile

 
Posted: 27 May 2009 08:25 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-06
226 posts

A library can accept __constructor parameters but the model doesn’t see to.  The alternative is to create an initialise() method that will accept your construct params.

class user_model extends model {
    
public function __construct()
    
{
        parent
::__construct();
    
}
    
public function initialise($param1$param2=NULL)
    
{
        $this
->param1 $param1;
    
}

then

$this->load->model('user_model');
$this->user_model->initialise('param1''param2'); 
 Signature 

Changing the world one line of code at a time

 
Posted: 27 May 2009 08:32 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2008-04-25
4956 posts

I second that. It’s always a good idea to have an initialisation method which you can call from the constructor if need be, although in your case, it’s pretty pointless if you’re loading the model via CodeIgniter’s loader.

I’d like to point out, however, that it’s probably wiser to stick to the US spelling, so your method should be called initialize().

 Signature 

http://www.phptherightway.com

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.

 
Posted: 15 December 2010 06:20 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2010-12-15
2 posts

Maybe someone is still interested in this issue. You can modify system library system/libraries/Loader.php, replace

function model($model$name ''$db_conn FALSE)
{
  
//...
  //...
    
$CI->$name = new $model();
    
$CI->$name->_assign_libraries();
    
$this->_ci_models[] $name;    

with

function model($model$name ''$db_conn FALSE)
{
  
//...
  //...

    
if (func_num_args() > 3{
        $refl 
= new ReflectionClass($model);
        
$CI->$name $refl->newInstanceArgs(array_slice(func_get_args(), 3));
    
else {
        $CI
->$name = new $model();
    
}
    $CI
->$name->_assign_libraries();
    
$this->_ci_models[] $name;    

Example model with arguments in construcrot:

class ExampleModel extends Model {

    
function __construct($param1$param2)
    
{
        parent
::Model();

        
// do some with params
    
}

    
//...

Now you can pass yours parameters from controller to a models constructor after the first three arguments

$this->load->model('ExampleModel'''FALSE$param1$param2); 

Sorry for my enslish smile

 
Posted: 01 March 2011 12:41 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2010-10-12
2 posts

I think this is an excellent extension - maybe improved only by making the 4th param an optional array, and then handling namespace variables like passage in views. 

Is this approach headed for core?

 
Posted: 27 May 2011 10:01 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-04-24
16 posts

This would really be a nice extension, i vote for making this another core element wink

 
Posted: 20 May 2012 04:42 PM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2009-07-01
27 posts

This helped me a lot, I second putting this in the core.

 Signature 

Web development blog

 
Posted: 06 January 2013 12:16 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2013-01-06
1 posts

Love it, has it been added yet?