Welcome Guest, Not a member yet? Register   Sign In
how to pass argument to model constructor while loading a model
#4

[eluser]lezeroq[/eluser]
Maybe someone is still interested in this issue. You can modify system library system/libraries/Loader.php, replace
Code:
function model($model, $name = '', $db_conn = FALSE)
{
  //...
  //...
    $CI->$name = new $model();
    $CI->$name->_assign_libraries();
    $this->_ci_models[] = $name;    
}
with
Code:
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:
Code:
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
Code:
$this->load->model('ExampleModel', '', FALSE, $param1, $param2);
Sorry for my enslish Smile


Messages In This Thread
how to pass argument to model constructor while loading a model - by El Forum - 12-15-2010, 04:20 AM



Theme © iAndrew 2016 - Forum software by © MyBB