- In CodeIgniter, the Model is used for the Database manipulation – fetch, insert, update, and delete records.
- If within the project there are multiple Models are available then it may require to perform the action during a Model which is already created in another Model.
- In this case, you’ll either create a separate function to perform an equivalent action or use the already available method within the Model.
- It is possible to reuse the method in the Model from another Model.
- Models are loaded within the Model as same as loaded within the Controller using $this->load->model().
- Create Three Models and call 1st and 2nd Model methods from the 3rd Model.
1.Configuration
Default controller
Open application/config/routes.php and edit default_controller value to User.
$route[‘default_controller’] = ‘User’;
2.Model 1 : Call Model method from another Model
Create a new Model_1.php file in application/models/ directory.
Create two methods –
- fun1()
- fun2()
which returns string text.
Code
[pastacode lang=”php” manual=”%3C%3Fphp%20if%20(%20!%20defined(‘BASEPATH’))%20exit(‘No%20direct%20script%20access%20allowed’)%3B%0A%0AClass%20Model_1%20extends%20CI_Model%20%7B%0A%0Apublic%20function%20fun1()%7B%0Areturn%20%22Model_1%20fun1%22%3B%0A%20%20%7D%0A%0Apublic%20function%20fun2()%7B%0Areturn%20%22Model_1%20fun2%22%3B%0A%20%20%7D%0A%0A%7D%0A%0A” message=”” highlight=”” provider=”manual”/]
3.Model 2 : Call Model method from another Model
Create a new Model_2.php file in application/models/ directory.
Create two methods –
- fun1()
- fun2()
which returns string text.
Code
[pastacode lang=”php” manual=”%3C%3Fphp%20if%20(%20!%20defined(‘BASEPATH’))%20exit(‘No%20direct%20script%20access%20allowed’)%3B%0A%0AClass%20Model_2%20extends%20CI_Model%20%7B%0A%0Apublic%20function%20fun1()%7B%0Areturn%20%22Model_2%20fun1%22%3B%0A%20%20%7D%0A%0Apublic%20function%20fun2()%7B%0Areturn%20%22Model_2%20fun2%22%3B%0A%20%20%7D%0A%0A%7D%0A%0A” message=”” highlight=”” provider=”manual”/]
4.Model 3 : Call Model method from another Model
Create a new Model_3.php file in application/models/ directory.
In this Model, I loaded 2 Models – Model_1 and Model_2 and access their methods.
- Syntax – Load and Access Model
$this->load->model(‘Model_name’); // Load Model
$this->Model_name->method_name(); // Access Method
- Load Models –
__construct – Load Model_1 and Model_2 using $this->load->model().
- Access Two methods –
fun1 – Call fun1() method of Model_1 $this->Model_1->fun1() and assign response in $response Array. Similarly call fun1() method of Model_2 $this->Model_2->fun1() and assign to $response Array.
Return $response.
- Access Single method –
fun2 – Call fun2() method of Model_2 $this->Model_2->fun2() and assign response in $response Array
Return $response.
Code
[pastacode lang=”php” manual=”%3C%3Fphp%20if%20(%20!%20defined(‘BASEPATH’))%20exit(‘No%20direct%20script%20access%20allowed’)%3B%0A%0AClass%20Model_3%20extends%20CI_Model%20%7B%0A%0Apublic%20function%20__construct()%20%7B%0A%20%20%20%20parent%3A%3A__construct()%3B%0A%0A%20%20%20%20%2F%2F%20Load%20Models%20-%20Model_1%20and%20Model_2%0A%20%20%20%20%24this-%3Eload-%3Emodel(‘Model_1’)%3B%0A%20%20%20%20%24this-%3Eload-%3Emodel(‘Model_2’)%3B%0A%20%20%7D%0A%0Apublic%20function%20fun1()%7B%0A%0A%20%20%20%20%2F%2F%20Access%20Two%20model%20methods%20and%20assing%20response%20to%20Array%0A%20%20%20%20%24response%5B%5D%20%3D%20%24this-%3EModel_1-%3Efun1()%3B%0A%20%20%20%20%24response%5B%5D%20%3D%20%24this-%3EModel_2-%3Efun1()%3B%0A%0A%20%20%20%20%24response%5B%5D%20%3D%20%22Model_3%20fun1%22%3B%0A%0Areturn%20%24response%3B%0A%20%20%7D%0A%0Apublic%20function%20fun2()%7B%0A%0A%20%20%20%20%2F%2F%20Access%20single%20Model%20method%20and%20assign%20response%20to%20Array%0A%20%20%20%20%24response%5B%5D%20%3D%20%24this-%3EModel_2-%3Efun2()%3B%0A%0A%20%20%20%20%24response%5B%5D%20%3D%20%22Model_3%20fun2%22%3B%0Areturn%20%24response%3B%0A%20%20%7D%0A%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
5.Controller
- Create a new User.php file in application/controllers/ directory.
- Load Model_3 Model and call fun1() and fun2() methods.
- Print the response.
Code
[pastacode lang=”php” manual=”%3C%3Fphp%0Adefined(‘BASEPATH’)%20OR%20exit(‘No%20direct%20script%20access%20allowed’)%3B%0A%0Aclass%20User%20extends%20CI_Controller%20%7B%0A%0Apublic%20function%20__construct()%7B%0A%0A%20%20%20%20parent%3A%3A__construct()%3B%0A%0A%20%20%20%20%2F%2F%20Load%20model%0A%20%20%20%20%24this-%3Eload-%3Emodel(‘Model_3’)%3B%0A%20%20%7D%0A%0Apublic%20function%20index()%7B%0A%0A%20%20%20%20%2F%2F%20Call%20fun1()%20method%20of%20Model_3%0A%20%20%20%20%24response1%20%3D%20%24this-%3EModel_3-%3Efun1()%3B%0Aecho%20%22%3Cpre%3E%22%3B%0Aprint_r(%24response1)%3B%0Aecho%20%22%3C%2Fpre%3E%22%3B%0A%0A%20%20%20%20%2F%2F%20Call%20fun2()%20method%20of%20Model_3%0A%20%20%20%20%24response2%20%3D%20%24this-%3EModel_3-%3Efun2()%3B%0Aecho%20%22%3Cpre%3E%22%3B%0Aprint_r(%24response2)%3B%0Aecho%20%22%3C%2Fpre%3E%22%3B%0A%0A%20%20%7D%0A%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
6.Output
It gives the following output –
Conclusion
Use $this->load->model() method to load the Model and access methods same as accessed from the Controller.