Welcome Guest, Not a member yet? Register   Sign In
Loading multiple models in a controller, which extend the same base class.
#1

[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


Messages In This Thread
Loading multiple models in a controller, which extend the same base class. - by El Forum - 05-07-2009, 10:54 AM



Theme © iAndrew 2016 - Forum software by © MyBB