CodeIgniter Forums
Extending Model class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Extending Model class (/thread-1971.html)



Extending Model class - El Forum - 07-09-2007

[eluser]marcalj[/eluser]
Making a CMS I have to extend controller and model class.

The controller class was easy but the model class don't.

I have to put
Code:
include_once( APPPATH . "libraries/CMS_Model.php" );
in the header of every specific model I create.

Why I need to include main model class and don't in the extended controller ??

Thanks.


Extending Model class - El Forum - 07-09-2007

[eluser]Glen Swinfield[/eluser]
You shouldn't have to do that.

Code:
class Mymodel extends Model
{
   // if you have a constructor make sure you call parent constructor
   function Mymodel(){
     parent::Model();
   }
}

// in controller

$this->load->model('Mymodel');

// Then use

$this->Mymodel->method();

Perhaps I have misunderstood the question?


Extending Model class - El Forum - 07-10-2007

[eluser]marcalj[/eluser]
...well I will explain in more detail the problem.

I have a library (CI model replacement):
Code:
class CMS_Model extends Model
{
    var $sTableName;
    
    function CMS_Model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function getNumRows()
    {
        return $this->db->count_all( $this->sTableName );
    }
...
}
located in " APPPATH . /libraries "

So all my model classes extends from CMS_Model:
Code:
class Projecte_model extends CMS_Model
{
    function Projecte_model()
    {
        // Call the Model constructor
        parent::CMS_Model();
        
        $this->sTableName = "projecte";
    }
...
}
This is how I make the controllers "generation".

The problem is that I have to include the file of CMS_Model definition and I don't why.

Now I suppose is better explanation Smile

Thanks.


Extending Model class - El Forum - 07-10-2007

[eluser]Glen Swinfield[/eluser]
Ok I see - I think this forum post as your answer(s):

http://ellislab.com/forums/viewthread/50986/


Extending Model class - El Forum - 07-10-2007

[eluser]marcalj[/eluser]
Thanks gms!! Smile

I was not able to find this post in the previous post search. Until there's no official solution the include_once will remain in the code Wink

See you and have a great coding day! jeje


Extending Model class - El Forum - 07-10-2007

[eluser]Mirage[/eluser]
Models are not supported for core class extension.

What's more, Models need to be in the 'models' directory if you want to load them [in your controller] using the Loader class. Those are the reasons you have to include it yourself. - which I don't find to be all that awful anyway.

If you want your CMS_Model to 'just be there' right from the start on every framework load, you have a couple of options:

1. Leave your model class file in the libraries folder and add it to the autoloader config.

2. The potential problem with #1 is that the autoloaded libraries are automatically instantiaded in the controller. Might not be an issue for you, but perhaps you don't like that. I certainly don't. So the option is to make it a helper or plugin and put it in those respective folders. There are no rules that say you can't have class definitions in helpers or plugins. So this will include the file, but not instantiate it.

3. #2 is a bit hacky as well. So why I did is to write a 'classloader' plugin, which I auto load. In that plugin I write include statements for all the class libs I need to load without instantiating them.

4. Instead of autoloading a plugin like I suggest in #3, you can write it as a pre-controller hook. I think that's probably most 'correct' way of handling this.

HTH