5

How do you set up CI2 to allow extending of multiple models?

I can only get it to extend one model (put in /application/core) named MY_Model (case sensitive).

To choose what model to extend I am doing; in the model..

require_once APPPATH.'core/MY_Another_model.php';
class Test_model extends MY_Another_model {
...
}

I can't find where in the core system code where it states only to allow models that are being extended to be called MY_Model.

Thank you for any and all help.

hakre
  • 193,403
  • 52
  • 435
  • 836
Rooneyl
  • 7,802
  • 6
  • 52
  • 81

4 Answers4

8

I've tried Cubed Eye's way, and it works, but here's another option:

Try adding a model to your autoload.php file. It could inherit from MY_Model (which inherits from CI_Model), and any additional models you load could inherit from it:

class Extended_model extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Another_model');
    }
}

(models/Extended_model.php)

class Another_model extends Extended_model {
}

(models/Another_model.php)

EDIT: I just realized that you're putting your extended model in the "core" folder. This is only necessary for classes that extend the core CI_* classes (i.e. MY_Controller, MY_Model, MY_Input, etc.). If you have a model extends MY_Model, put it in your models/ folder instead, and don't prefix it with "MY_".

landons
  • 9,502
  • 3
  • 33
  • 46
  • Even though I have accepted the above answer, I have voted this up as I like having separate files per model, instead of stuffing them all in one file. – Rooneyl Nov 15 '11 at 14:23
4

As I said in this question about the controllers you just put both classes in the same MY_Model file. This file is used as part of the autoload feature of codeigniter, meaning that it will look for any files with the MY_ (or config defined) prefix.

You don't even need to call the class inside MY_Model you can potentially call it MY_Special_Model and have MY_Another_Model directly underneath

Community
  • 1
  • 1
Cubed Eye
  • 5,581
  • 4
  • 48
  • 64
  • This is exactly what I do! It's not pretty because I like having each class in a separate file ... but it's the only way I could achieve this in CI. – MikeMurko Nov 17 '11 at 17:29
  • @MikeMurko There is way to have separate classes in separate files, but it involve making a custom loader class and isn't worth the time and effort. – Cubed Eye Nov 17 '11 at 22:34
  • I have following class in MY_Model.php: class Public_Model extends CI_Model { function __construct() { parent::__construct(); } } But it show me following error: "MY_Model.php exists, but doesn't declare class MY_Model" What should I do? By the way, I am on CodeIgniter 3.1 – Usman Ahmed Aug 27 '16 at 07:16
1

Eventually you may try this hack, using load_class function:

This is your model extension:
application/core/Special_model_class.php:

class CI_Special_model_class extends CI_Model {...}

The name of the php file is without the CI_ prefix, although the class itself is with prefix!

The model based on this class within the models/ folder:
application/models/one_model.php:

class One_model extends CI_Special_model_class {...}

In order to make this work, you should call the load_class function from the controller:
application/controllers/one_ctrl.php

....
load_class('Special_model_class', 'core');
$this->load->model('Special_model_class');
....

Eventually, you may try to call load_class within the model, right before defining it. application/models/one_model.php:

load_class('Special_model_class', 'core');
class One_model extends CI_Special_model_class {...}
Mxyk
  • 10,678
  • 16
  • 57
  • 76
Zoltan
  • 11
  • 1
0

Here is the parent model class:

class MY_Common_Model extends CI_Model {

    function __construct() {
        parent::__construct();
    }
        function drop_table($table_name) {
        $this->connect();
         $this->dbforge->drop_table($table_name);

    }

}

Here is the child model class:

class MY_Model extends MY_Common_Model {
     function inset_table($table_name) {
        $this->connect();
        $this->insert_table($table_name);

    }
}

in model:

 $this->drop_table($edge_table);
ShivarajRH
  • 902
  • 12
  • 24