• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extending the default model

#11
[eluser]roguewave[/eluser]
Thanks - that will work for me I think.

#12
[eluser]wiredesignz[/eluser]
CI's Model is just an empty class, there is no need to extend it, simply copy system/libraries/Model.php to application/libraries/Model.php and add your own functionality (CRUD)

#13
[eluser]treehousetim[/eluser]
[quote author="wiredesignz" date="1218817030"]CI's Model is just an empty class, there is no need to extend it, simply copy system/libraries/Model.php to application/libraries/Model.php and add your own functionality (CRUD)[/quote]

Future-proofing code makes it better to extend than replace - if CI fixes bugs or adds functionality of their own to models that comes for free when extending. If one copies and replaces the model, then upgrades are more than replacing the system folder. That's the main reason I chose to extend.

#14
[eluser]m4rw3r[/eluser]
Personally I don't extend or USE the default model (with IgnitedRecord).
Instead I add this:
Code:
class Amodel{
    function Amodel(){
        // just fetch the database instance
        $CI =& get_instance();
        $this->db =& $CI->db;
    }
    /**
     * To fool CI, letting it think this is a proper model.
     */
    function _assign_libraries(){}
}
The reason I do this is that I only use the db object, and I think it is better to restrict interaction with the controller object (and print_r becomes a mess).

This with extending CI's Model class, I think it can be done somewhat nicely like this:
CI_Model class in system/libraries/Model.php
MY_Model class in app/libraries/MY_Model.php
either conditional require of a class named Model which extends one of those above (depends on if MY_Model exists), or like in the db class, conditional eval of such a class.

#15
[eluser]kirrie[/eluser]
[quote author="m4rw3r" date="1218845818"]Personally I don't extend or USE the default model (with IgnitedRecord).
Instead I add this:
Code:
class Amodel{
    function Amodel(){
        // just fetch the database instance
        $CI =& get_instance();
        $this->db =& $CI->db;
    }
    /**
     * To fool CI, letting it think this is a proper model.
     */
    function _assign_libraries(){}
}
The reason I do this is that I only use the db object, and I think it is better to restrict interaction with the controller object (and print_r becomes a mess).

This with extending CI's Model class, I think it can be done somewhat nicely like this:
CI_Model class in system/libraries/Model.php
MY_Model class in app/libraries/MY_Model.php
either conditional require of a class named Model which extends one of those above (depends on if MY_Model exists), or like in the db class, conditional eval of such a class.[/quote]

I think this is the best way to use a custom model instead of a default CI model. It works great. Above all things, that I don't need to see a mess of print_r($this) anymore is super-fantastic to me. Smile thanks!

#16
[eluser]John_Betong[/eluser]
[quote author="dieter" date="1213192946"]Is it possible to extend CI's default Model? I've tried creating an extending MY_Model in app\libraries\ but I get an error saying that the class can't be found. Somewhere else on the forum, I found a hack to solve this problem by adding a require_once in every model, but shouldn't this be handled by CI itself (that's why we use a framework after all). Or am I overseeing something?

Thanks,
Dieter[/quote]
 
http://ellislab.com/forums/viewthread/73971/
 
Take a look at this thread and I reckon it could solve your problem.
 
 


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.