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

[eluser]Nick Jennings[/eluser]

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:

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):

include dirname(__FILE__)."/ma_base.php";
class Website extends MA_Base {

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

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

        $data['client_info'] = $this->Client_model->getListBrief($data['view']['client_id']);

include dirname(__FILE__)."/ma_model.php";

class Website_model extends MA_Model {

    function Website_model() {
        // Call the Model constructor
        $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();


class MA_model extends Model {

    function MA_model($table) {
        // Call the Model constructor
        $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!

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