7

These is the controller

class Dashboard extends CI_Controller{
public function __construct(){
    parent::__construct();
    $this->load->model("admin/post_model");
    $this->load->model("admin/comment_model");
}
    public function index(){
    $data['post_res'] = $this->post_model->getPost();
    $data['com_res']  = $this->post_model->getComments();
    }
 }

I cannot load 2 model in the same controller. It gives me an error

Fatal error: Call to a member function getComments() on a non-object in C:\xampp\htdocs\blog\application\controllers\ram-admin\dashboard.php on line 13

How can I possibly load the models?

Thanks you so much in advance!

tereško
  • 58,060
  • 25
  • 98
  • 150
user1033600
  • 289
  • 4
  • 7
  • 17
  • 2
    You CAN load two models in the same controller, I did it plenty of times. The issue is another – Damien Pirsy Jun 06 '12 at 07:15
  • Damien Pirsy is right it is a common practice to load multiple models in a single controller you should check your model file or post the model code here..... – Code Prank Jun 06 '12 at 08:54
  • possible duplicate of [Codeigniter - I am looking to use/connect to a different database for one of my controllers and one model](http://stackoverflow.com/questions/312511/codeigniter-i-am-looking-to-use-connect-to-a-different-database-for-one-of-my) .. also: http://stackoverflow.com/a/12769983/727208 – tereško Feb 12 '13 at 21:50
  • I'm having the same problem. I'm getting the same error. I thought it must be something else, but now I've narrowed it down to "I think" having multiple controllers. Hence, I am here. – TARKUS Oct 07 '13 at 00:37

6 Answers6

6

Try this

class Dashboard extends CI_Controller {
 function __construct() {
      parent::__construct(); 

    $this->load->model("admin/post_model","post_model");
    $this->load->model("admin/comment_model","comment_model");
  }

public function index(){

    $data['post_res'] = $this->post_model->getPost();
    $data['com_res']  = $this->comment_model->getComments();
}
Query Master
  • 6,989
  • 5
  • 35
  • 58
  • The second argument to the `model` method is only required if you want the loaded model to have a different name locally. In your example you could have omitted the 2nd arguments altogether and achieved the same result. – Madbreaks Dec 23 '15 at 23:00
3

Check if the model correctly extends the CI_Model for comment_model and post_model

example:

  class comment_model extends CI_Model{    

   }

  class post_model extends CI_Model{    

   }
laxonline
  • 2,657
  • 1
  • 20
  • 37
Faizan Khattak
  • 862
  • 8
  • 22
  • This answers this question exactly ! – Jimmy Obonyo Abor Feb 26 '15 at 22:30
  • I had this error and it was very difficult to spot. I created a Model and incorrectly extended `CI_Controller`. It worked fine weirdly, but not when I tried to load it with more models in the same controller. – Nahuel Jun 09 '16 at 18:03
2

getComments() is comment_model, not post_model..

You can name your models by passing a second parameter;

$this->load->model('admin/comment_model', 'comments');
$data['com_res'] = $this->comments->getComments();
Rooneyl
  • 7,802
  • 6
  • 52
  • 81
0

This is weird

I just put this line of code

 $this->load->model("admin/comment_model","comment_model");

before this one

 $this->load->model("admin/page_model","page_model");

And it works fine now :)

Thank you for all the response!

user1033600
  • 289
  • 4
  • 7
  • 17
-1

For multiple models, you can do this:

$models = array(
    'menu_model' => 'mmodel',
    'user_model' => 'umodel',
    'admin_model' => 'amodel',
);

foreach ($models as $file => $object_name)
{
    $this->load->model($file, $object_name);
}

But as mentioned, you can create file application/core/MY_Loader.php and write your own method for loading models. I think this might work (not tested):

class MY_Loader extends CI_Loader {

    function model($model, $name = '', $db_conn = FALSE)
    {
        if (is_array($model))
        {
            foreach ($model as $file => $object_name)
            {
                // Linear array was passed, be backwards compatible.
                // CI already allows loading models as arrays, but does
                // not accept the model name param, just the file name
                if ( ! is_string($file)) 
                {
                    $file = $object_name;
                    $object_name = NULL;
                }
                parent::model($file, $object_name);
            }
            return;
        }

        // Call the default method otherwise
        parent::model($model, $name, $db_conn);
    }
}

Usage with our variable from above:

$this->load->model($models);

You could also allow a separate DB connection to be passed in an array, but then you'd need to have a multidimensional array, and not the simple one we used. It's not too often you'll need to do that anyways.

Rohan Patil
  • 2,328
  • 1
  • 16
  • 23
  • 1
    That's great, but are you sure it's necessary to mess with the core just to call 2 models? I often call 2 or more models and never had issues – Damien Pirsy Jun 06 '12 at 07:20
  • This is just an alternative method to use multiple models. Is your getComments() function in the comment_model? If so then only change $data['com_res'] = $this->post_model->getComments(); to $data['com_res'] = $this->comment_model->getComments(); Dont need to do above – Rohan Patil Jun 06 '12 at 07:22
  • This is completely unnecessary and doesn't answer the original question. – Madbreaks Dec 23 '15 at 23:04
-1

Just use the model names in an array like above.

$this->load->model(array("admin/post_model", "admin/comment_model"));
j0k
  • 22,600
  • 28
  • 79
  • 90