How to Inherit A Model from Another Model in CodeIgniter
Asked Answered
H

5

12

i'm using codeigniter for my project and i have this class model which i call Genesis which looks like this:

class Genesis_model extends CI_Model {
    function __construct() {
        parent::__construct();
    }

    function get() {
        return 'human soul';
    }
}

and i have another model, stored in the same directory, which extends Genesis_model

class Human_model extends Genesis_model {
    function __construct() {
        parent::__construct();
    }

    function get_human() {
        return $this->get();
    }
}

Human_model is used by Human controller

class Human extends CI_Controller {     
    function __construct(){
        parent::__construct();
        $this->load->model('human_model');
    }       

    function get_human() {
        $data['human'] = $this->human_model->get_human();
        $this->load->view('human/human_interface', $data);
    }
}

if i execute the code, it will produce an error which point to return $this->get(). it reads "Fatal error: Class 'Genesis_model' not found in ...\application\models\human_model.php on line 2".

i use this method because nearly all my models shared almost identical structure. I gather the similar functionality in Genesis while the other models serve only as data suppliers unique to the tables they represent. it works well in my asp.net (vb.net) but i don't how to do it in codeigniter.

is there a way for Human_model to inherit Genesis_model. i don't think i'm allowed to use include('genesis_model.php'). i don't know if it works either.

thanks in advance.

Holcomb answered 4/7, 2011 at 8:56 Comment(1)
interesting answers here #46838Retort
C
7

Put the file genesis_model.php in the core directory

Chihli answered 4/7, 2011 at 20:36 Comment(2)
Yes. You can create as many extensions to your models as you need and simply place it in /core/ and use them at your convenience.Chihli
I just moved genesis_model.php to /core/ and renamed the file name and the class name as MY_Controller.php and MY_Controller. but the execution stops at $this->get() in Human_model with error: "Fatal error: Call to undefined method Human_model::get() in ..\application\models\human_model.php" did i miss something?Holcomb
H
8

core/MY_Model is good if there's only 1 important superclass for your models.

If you want to inherit from more than model superclass, a better option is to change your autoload configuration.

In application/config/autoload.php, add this line:

    $autoload['model'] = array('genesis_model');
Harty answered 30/11, 2013 at 1:35 Comment(4)
I believe this answer is exactly what the author of the question wants. I already have a MY_Model and want to extent that MY_Model in ME_Model. By placing the ME_Model in application/models and autoloading it as suggested above, I can achieve this goal.Earthly
Which directory does autoload look for? I put my second model class in the core directory and put the class name in autoload, an error said that the class can not be found.Harsh
@Harsh This will load any available model in your application, usually in application/models/.Cimbri
Also instead of the autoload method, you can use $this->load->model('Genesis_model'); if you ALWAYS load the extended Genesis_model before loading the model Human_model. For example, this would be better if you only load the two models within the same library constructor.Cimbri
C
7

Put the file genesis_model.php in the core directory

Chihli answered 4/7, 2011 at 20:36 Comment(2)
Yes. You can create as many extensions to your models as you need and simply place it in /core/ and use them at your convenience.Chihli
I just moved genesis_model.php to /core/ and renamed the file name and the class name as MY_Controller.php and MY_Controller. but the execution stops at $this->get() in Human_model with error: "Fatal error: Call to undefined method Human_model::get() in ..\application\models\human_model.php" did i miss something?Holcomb
C
5

Change your Human_model to this:

include('genesis_model.php');
class Human_model extends Genesis_model {
    function __construct() {
        parent::__construct();
    }

    function get_human() {
        return parent::get();
    }
}

notice the get_human function and the include.

Chalutz answered 4/7, 2011 at 13:2 Comment(0)
C
2

You have to include the Genesis_model on your Human_model.php like this:

include_once( APPPATH . 'folder/file' . EXT );

Or you can autoload it on your config/autoload.php file, what I think is stupid =)

Crapulous answered 4/7, 2011 at 12:45 Comment(0)
O
2

other solution

<?php
$obj = &get_instance();
$obj->load->model('parentModel');
class childModel extends parentModel{
    public function __construct(){
        parent::__construct();
    }

    public function get(){
        return 'child';
    }
}
?>
Orlon answered 1/9, 2014 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.