CodeIgniter loads multiple models into one controller

This 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 can not load 2 models in one 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 load models?

Thank you for this in advance!

+4
source share
6 answers

try it

 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(); } 
+5
source

getComments () 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(); 
+2
source

Verify that the CI_Model model CI_Model for comment_model and post_model

An example :

  class comment_model extends CI_Model{ } class post_model extends CI_Model{ } 
+2
source

This is strange

I just put this line of code

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

before

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

And now it works great :)

Thanks for the whole answer!

0
source

For several 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 already mentioned, you can create the file application / core / MY_Loader.php and write your own method for loading models. I think this might work (not verified):

 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); } } 

Using our variable on top:

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

You can also allow the transfer of a single DB connection to an array, but then you need a multidimensional array, not the simple one we used. This is not too often, you still have to do it.

-1
source

Just use the model names in the array as above.

 $this->load->model(array("admin/post_model", "admin/comment_model")); 
-1
source

Source: https://habr.com/ru/post/1416306/


All Articles