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 in a Model which is already created in another Model.
In this case, you can either create a separate function to perform the same action or use the already available method in the Model.
It is possible to reuse the method in the Model from another Model.
Models are loaded in the Model as same as loaded in the Controller using $this->load->model()
.
In this tutorial, I create 3 Models and call 1st and 2nd Model methods from the 3rd Model.
Contents
1. Configuration
Default controller
Open application/config/routes.php
and edit default_controller
value to User
.
$route['default_controller'] = 'User';
2. Model 1
Create a new Model_1.php
file in application/models/
directory.
Create two methods – fun1()
and fun2()
which returns string text.
Completed Code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Class Model_1 extends CI_Model { public function fun1(){ return "Model_1 fun1"; } public function fun2(){ return "Model_1 fun2"; } }
3. Model 2
Create a new Model_2.php
file in application/models/
directory.
Create two methods – fun1()
and fun2()
which returns string text.
Completed Code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Class Model_2 extends CI_Model { public function fun1(){ return "Model_2 fun1"; } public function fun2(){ return "Model_2 fun2"; } }
4. Model 3
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
.
Completed Code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Class Model_3 extends CI_Model { public function __construct() { parent::__construct(); // Load Models - Model_1 and Model_2 $this->load->model('Model_1'); $this->load->model('Model_2'); } public function fun1(){ // Access Two model methods and assing response to Array $response[] = $this->Model_1->fun1(); $response[] = $this->Model_2->fun1(); $response[] = "Model_3 fun1"; return $response; } public function fun2(){ // Access single Model method and assign response to Array $response[] = $this->Model_2->fun2(); $response[] = "Model_3 fun2"; return $response; } }
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.
Completed Code
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class User extends CI_Controller { public function __construct(){ parent::__construct(); // Load model $this->load->model('Model_3'); } public function index(){ // Call fun1() method of Model_3 $response1 = $this->Model_3->fun1(); echo "<pre>"; print_r($response1); echo "</pre>"; // Call fun2() method of Model_3 $response2 = $this->Model_3->fun2(); echo "<pre>"; print_r($response2); echo "</pre>"; } }
6. Output
It gives the following output –
7. Conclusion
Use $this->load->model()
method to load the Model and access methods same as accessed from the Controller.