How to call model function from another model in Codeigniter?

How to call model function from another model in Codeigniter?

In this post we will give you information about How to call model function from another model in Codeigniter?. Hear we will give you detail about How to call model function from another model in Codeigniter?And how to use it also give you demo for it if it is necessary.

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:

Also see:How to create dynamic Highcharts in Codeigniter 3?

Array

(

[0] => Model_1 fun1

[1] => Model_1 fun2

[2] => Model_2 fun1

)

I hope it can help you….

Hope this code and post will helped you for implement How to call model function from another model in Codeigniter?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

2  +  7  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US