How to Call Model Function from Another Model in Codeigniter?
We may sometime require to call model method from another model in codeigniter app. i will show you how to call model function to another model, same model and controller file.
we always use model for database logic but i require to use for some classes like as helper. but i have more then one class as group wise and i require to call some class function to another class.
So finally i found the way to call model function to another model using "$this->load->model('Model Class Name')". I also want to give you small and quick example to call model function to another model or controller file.
In this example, i am going to create two model with Model_1 and Model_2, then i use Model_1 function to Model_2 model class. After that i created one BaseController file and class Model_2 method and print it. So let's see example:
Create Model_1 Model File:
first of all, we will create "Model_1" Model class and declare two method as fun1() and fun2(). So let's create.
application/models/Model_1.php
<?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";
}
}
Create Model_2 Model File:
next, we will create "Model_2" Model class and declare one method as fun1() and use Model_1 class function on it. So let's create.
application/models/Model_2.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Model_2 extends CI_Model {
public function __construct() {
parent::__construct();
/* Load Models - Model_1 */
$this->load->model('Model_1');
}
public function fun1(){
/* Access Two model methods and assing response to Array */
$response[] = $this->Model_1->fun1();
$response[] = $this->Model_1->fun2();
$response[] = "Model_2 fun1";
return $response;
}
}
Create Route:
now, we will create one dummy route for testing so we can test it on our browser. So let's add route like as bellow:
application/config/routes.php
$route['base-call'] = "BaseController";
Create BaseController:
at last, we will create one controller "BaseController" with index method. So let's add controller file code like as bellow:
application/controller/BaseController.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class BaseController extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('Model_2');
}
public function index(){
$response1 = $this->Model_2->fun1();
echo "<pre>";
print_r($response1);
echo "</pre>";
}
}
Now you can simply run this example and check output.
output will be like as bellow:
Array
(
[0] => Model_1 fun1
[1] => Model_1 fun2
[2] => Model_2 fun1
)
I hope it can help you....
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Codeigniter Form Submit using Ajax Request Example
- How to Get Last Executed Query in Codeigniter?
- Codeigniter Dynamic Highcharts Example
- Codeigniter Implement Fullcalendar Example Tutorial
- Codeigniter Create Zip File and Download Example
- Codeigniter Create Dynamic Tree View using Treeview JS
- Codeigniter Google Recaptcha Form Validation Example
- Codeigniter Ajax Pagination using JQuery Example
- Codeigniter Delete Multiple Rows using Checkbox Example
- Codeigniter JQuery Ajax Autocomplete Search using Typeahead
- Codeigniter JQuery Ajax Request Example
- Codeigniter Confirm Box Before Delete Record Example
- How to Create Dynamic Sitemap in Codeigniter?
- Codeigniter Generate PDF from View using Dompdf Example