19

I am using the following code to initialize a model from within my controller:

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

Is it possible to modify the above line somehow so that the model constructor recieves a parameter? I want to use the following code in the model constructor:

function __construct($param_var) {
   parent::Model();

   $this->$param_var = $param_var; //I'm not even sure this works in PHP..but different issue
}

This would be very helpful so that I can reuse my model classes. Thanks.

UPDATE: (from one of the answers, my original question is solved..thanks!) Just to explain why I wanted to do this: the idea is to be able to reuse a model class. So basically to give a simple example I would like to be able to pass an "order_by" variable to the model class so that I can reuse the logic in the model class (and dynamically change the order-by value in the sql) without having to create a separate class or a separate function.

Is this poor design? If so could you please explain why you wouldn't do something like this and how you would do it instead?

2
  • I am curious to why you would want to pass a parameter to your Model's constructor. My guess is that you need a library instead of a model. Commented Jul 19, 2009 at 3:07
  • 1
    i just updated my question with an explanation as to why I would do this Commented Jul 19, 2009 at 5:14

5 Answers 5

23

You can't pass parameters through the load function. You'll have to do something like:

$this->load->model('model_name');
$this->model_name->my_constructor('stuff');

In the model:

function my_constructor($param_var) {
...
}

Response to update:

You could just pass the order_by value when you're calling your model function. I'm assuming in your controller action, you have something like $this->model_name->get($my_id); Just add your order_by parameter to this function. IMO this makes your model logic more flexible/reusable because the way you were doing it, I assume setting order_by in the constructor will set the order_by value for every function.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks and don't forget you can set default values $param_var=true in the model function too.
Great answer! However, CodeIgniter is somehow blocking the way on this paradigm.
4

In model

<?php

/* Load Model core model */
/* BASEPATH = D:\xampp\htdocs\ci_name_project\system\ */
include BASEPATH . 'core\\Model.php';

class User_model extends CI_Model {

    /* Properties */
    private $name;


    /* Constructor parameter overload */
    public function __construct($name) {
        $this->set_name($name);
    }    


    /* Set */
    public function set_name($name) {
        $this->name = $name;
    }


    /* Get */
    public function get_name() {
        return $this->name;
    }

}

in controller

<?php

class User_controller extends CI_Controller {

    public function index() {

        /* Load User_model model */
        /* APPPATH = D:\xampp\htdocs\ci_name_project\application\ */
        include APPPATH . 'models\\User_model.php';

        $name = 'love';

        /* Create $object_user object of User_model class */
        $object_user = new User_model($name);     

        echo $object_user->get_name(); // love

    }

}

Comments

3

I see your reasoning for this, but may I suggest looking at Object-Relational Mapping for your database needs. There is a user-made ORM library for CodeIgniter called DataMapper that I've been using lately. You can use tables in your controllers as objects, and it may be a better fit for your problem.

2 Comments

thanks for the reference, but I think this might be overkill..I know I didnt give much of a description of my application, but when would you recommend using this ORM library? Wouldn't the application have to be very data intensive (in terms of db access) to warrent this?
In a way yes, I like to use it because it does lead to less code if your app is somewhat data intensive, but unless your db is really small it may be a bit overkill.
1

Instead of using DataMapper i suggested to use IgnitedRecord because that the DataMapper is no longer maintained more over it has been replaced into Ruby

Comments

0

I am using CI ver 3.X, so what I am about to say is it will work for Codeigniter 3.X (and I haven't checked ver 4+ yet).

When I went thru the source code of the function model() in file system/libraries/Loader.php, noticed that it does not support loading the model with construct parameters. So if you want to make this happen you have to change the source code (bold, I know, and I just did).

Down below is how I did it.

1. Firstly, replace line 355

$CI->$name = new $model();

with some modifications:

$_args_count = func_num_args();
if(3 < $_args_count){
    $refl = new ReflectionClass($model);
    $CI->$name = $refl->newInstanceArgs(array_slice($_args_count, 3));
}else{
    $CI->$name = new $model(); // origin source code 
}

2. Load the model with a bit difference:

$this->load->model("model_name", "model_name", false, $param_var); // where amazing happens

Now you can have $this->model_name as you wished.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.