Welcome Guest, Not a member yet? Register   Sign In
Multiple subclasses of Model
#1

[eluser]peterbz[/eluser]
Is it possible to have more than one subclass of the Model (and other core classes of CI). In the documentation, it says that the subclass must be prefixed with MY_ which means you can only have one subclass of Model (MY_Model). What if I want to have several subclasses of the model.

This is because I would like to have different types of Models. For example, I want to create 2 subclasses of the model: Listing_Model and Vendor_Model. Listing_Model will be extended by classes Car_Listing_Model, Truck_Listing_Model, etc and Vendor_Model will be extended by Dealership_Model, Part_Shop_Model, etc

In other words, I want to extend from a model I've created. So in the above example, Listing_Model extends Model and Car_Listing_Model extends Listing_Model.
#2

[eluser]peterbz[/eluser]
Now, if you're asking why I would want to do this, it's because for example, the Car_Listing_Model and Truck_Listing_Model share many same methods and should be both extended from Listing_Model.
#3

[eluser]TheFuzzy0ne[/eluser]
I don't understand the problem. You are able have a model which inherits like this: Car_Listing_Model -> Listing_Model -> Model

You just need to ensure you've either loaded the model you're extending beforehand, or just require_once() into the script.
#4

[eluser]Nevio[/eluser]
peterbz,

Factory pattern. Search for it on google or you can use __get and __set magic php methods for getting sub-classes...

For example ( not sure if this works and there is better way just I don't have much time to write it down )

Code:
class Modelfactory extends Model{
    
     private $_container = NULL;
    
     function __construct()
     {
         $this->_container = new stdClass();  
         $this->_loadModelOne();
     }

     private function _loadModelOne()
     {
         if(! @is_object( $this->_container->modelone ) )
         {
             $this->load->model( 'modelfactory/modelone' );
             $this->_container->modelone = $this->modelone;
         }
     }
    
     private function __get( $name )
     {
         if(! @is_object( $this->_container->$name ) )
             show_error ( "Model name {$name} doesn't exists!" );
        
         return  $this->_container->$name;
     }
          
}

// lets go :D
$this->load->model( 'modelfactory' );
var_dump( $this->modelfactory->modelone->dumpSomething() );
#5

[eluser]peterbz[/eluser]
[quote author="TheFuzzy0ne" date="1238732040"]I don't understand the problem. You are able have a model which inherits like this: Car_Listing_Model -> Listing_Model -> Model

You just need to ensure you've either loaded the model you're extending beforehand, or just require_once() into the script.[/quote]

So would this be the proper way:

Code:
<?php
  
  class Listing_Model extends Model{
    function __construct(){
      parent::Model();
    }

    function get_listings(){
      //code code code
    }
  }

Code:
<?php

  require_once("path/to/Listing_Model.php");
  
  class Car_Listing_Model extends Listing_Model{
    function __construct(){
      parent::Listing_Model();
    }
  
  }



@ Nevio,

I don't think I need a factory pattern for this case. What I'm trying to do is not regulating the instances but I'm having trouble extending from my Model.
#6

[eluser]jedd[/eluser]
peterbz - it's not the same thing, but I asked a related question a while back on how closely aligned a CI model is to a Real PHP Object (or rather, an OO paradigm concept of an Object) - and came to some .. erhm .. pragmatic conclusions.

You can [url="http://ellislab.com/forums/viewthread/108763/"]read the thread[/url] yourself if you're interested.
#7

[eluser]peterbz[/eluser]
Problem solved.

In my Car_Listing_Model __construct, I put parent::Listing_Model.
It should be instead parent::__construct

Thanks for your help.
#8

[eluser]_Dannoso_[/eluser]
[quote author="peterbz" date="1238742950"]Problem solved.

In my Car_Listing_Model __construct, I put parent::Listing_Model.
It should be instead parent::__construct

Thanks for your help.[/quote]

I'm searching for the same solution.

I think it's enough load your extended base class Listing_Model before Car_Listing_Model.
This way you shouldn't need the include command neither. But... just try!

Dannoso
#9

[eluser]peterbz[/eluser]
Yes, one of the many things that really bugs me in CI is the autoload. All classes must be loaded in sequence. I think a feature in the near future that CI could implement is to truly autoload classes without defining them. Such as everytime an object is instantiated, it will automatically look for the classes in the respective folders (like how symfony does it).
#10

[eluser]_Dannoso_[/eluser]
This is due to poor PHP4 OO implementation!
CI derived platform Kohana, requiring php 5, do that!

Bye!




Theme © iAndrew 2016 - Forum software by © MyBB