Download and use the codeigniter model from another model

Colleagues using codeigniter 1.7.3, can I load a model from the code of another model? I read a lot of theoretical and practical messages, but no one gave a definitive answer.

I have a model that has a function in which I would like to perform an operation on another model. The code is as follows:

1: $this->load->model('decision_model'); 2: $this->decision_model->hello_decision(); 

line 1 works. line 2 does not work as follows:

PHP error was an error
Severity Level: Notification
Message: Undefined property: Account_model :: $ decision_model
File Name: models / account_model.php

I tried to create simple dumb models, changed function names, giving the model an alias at boot, etc .... no luck

So, theory aside, is this doable?

early.

+8
codeigniter model
source share
4 answers

You can do it as follows:

 class User_model extends Model { function get_something() { $CI =& get_instance(); $CI->load->model('profile_model'); return $CI->profile_model->get_another_thing(); } } 
+13
source share

In CI 2.0, you can simply call one model directly from another.

+3
source share

Try the following:

 $this->load->model('decision_model'); $CI =& get_instance(); $CI->decision_model->hello_decision(); 
0
source share

You can also add the class variable private $_ci; and initialize it in your constructor.

 public function __construct($input=null) { $this->_ci =& get_instance(); if ( $input != null && is_array($input) ) { $this->populate($input); } } 

Then it will be available for any function that you work with, no need get_instance() everywhere.

0
source share

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


All Articles