CodeIgniter Forums
Loading multiple models in a controller, which extend the same base class. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Loading multiple models in a controller, which extend the same base class. (/thread-18473.html)



Loading multiple models in a controller, which extend the same base class. - El Forum - 05-07-2009

[eluser]Nick Jennings[/eluser]
Hello,

I have 2 tables in a database, 'website' and 'client'. Each table has it's own model which extends a "base" class (MA_Model) - this base class provides methods which are identical between the two models (things like generic select *'s and getting a record based on ID, etc.).

This works fine, until I try to load both the 'website' and 'client' models in the same controller function. When I try to load the second model, I get the following error in the apache logs:


Code:
PHP Fatal error:  Cannot redeclare class MA_model in <ci_path>/models/ma_model.php on line XXX,
referer: http://localhost/index.php/website

(NOTE: line XXX always equals the closing bracket of the MA_Model class definition ie. end of file)


I'm not sure why I can extend the base CI 'Model' class for every class I make, however when I make my own 'MA_Model' class, I can't load two models at once which both extend the class. Am I doing something wrong? Here is a snippet of my code (simplified for purpose of posting):


controllers/website.php
Code:
&lt;?php
include dirname(__FILE__)."/ma_base.php";
class Website extends MA_Base {

    function Website() {
        parent::MA_Base();
        $this->load->model('Website_model', 'DB');
    }

    function view($id = 0) {
        $data['view'] = $this->DB->getRecordById($id);

        $this->load->model('Client_model');
        $data['client_info'] = $this->Client_model->getListBrief($data['view']['client_id']);
        
        $this->load->view('template',$data);
    }
}


models/website_model.php
Code:
&lt;?php
include dirname(__FILE__)."/ma_model.php";

class Website_model extends MA_Model {

    function Website_model() {
        // Call the Model constructor
        parent::MA_Model('website');
        $this->TABLE = 'website';
    }

    function getListBrief() {
        $sql = "SELECT `id`,`client_id`, `date_created`,`website_status`,`permanent_url`".
               "FROM `$this->TABLE` WHERE `is_deleted` != '1'";
        $query = $this->db->query($sql);
        return $query->result_array();
    }


models/ma_base.php
Code:
&lt;?php

class MA_model extends Model {

    function MA_model($table) {
        // Call the Model constructor
        parent::Model();
        $this->TABLE = $table;

    }

    function getRecordById($id) {
        $data = array();
        $sql = "SELECT * FROM `".$this->TABLE."` WHERE `id` = '".$id."'";
        $query = $this->db->query($sql);

        if ($query->num_rows() > 0) {
            $data = $query->row_array();
        }
        return $data;
    }


Thanks in advance for any help!
-Nick


Loading multiple models in a controller, which extend the same base class. - El Forum - 05-07-2009

[eluser]drewbee[/eluser]
You can extend the base model class as many times as you like because it is only been declared once, but initiated many times.

Declared = file has been loaded through a require/include etc.
Initiated = class has been initiated into an instance of the class and assigned a var.

For your include, you simply need to check and see if the class has been loaded yet:
I believe class_exists checks against declared classes, so you should be able to use that:


replace:
Code:
include dirname(__FILE__)."/ma_model.php";

With:

Code:
if (!class_exists('MA_Model', false))
{
    include dirname(__FILE__)."/ma_model.php";
}

Now you should just have to put this on each of your files where you want to use multiple models in the same controller.