Extending the default model |
[eluser]dieter[/eluser]
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
[eluser]Michael Wales[/eluser]
Yes - you can extend the default model (I do it on all my application with my ORM library, which I'll be releasing soon). libraries/MY_Model.php: Code: class Orm extends Model {
[eluser]koorb[/eluser]
I think the problem is dieter is seeing is caused by this http://codeigniter.com/bug_tracker/bug/3102/. Model is not loaded like other core libraries and cannot be extended using the MY_x extends CI_x method. Im not sure why it is done this way, one of the mysteries of CI. But it does cause problems I am currently trying to add some basic common methods to all my models and therefore would like to extend Model to implement them. I will let you know what I end up doing. @dieter: I'm presuming the class that can't be found is 'Model'. The reason you'll get that is because of the way Model is loaded like I mentioned above, but if you require BASEPATH . '/libraries/Model.php' in your MY_Model.php that should work. Edit: Incidentally I don't seem to be able to use __get and __set methods because CI instance properties are assigned to Model by reference and PHP wont allow referenced attributes on overloadable classes
[eluser]treehousetim[/eluser]
if you create library/MY_Model.php... Code: class MY_Model Then in your regular model files, you need to extend from MY_Model... Code: class myTable extends MY_Model This works - I'm using it all the time. If you continue to get errors, please send more info.
[eluser]wiredesignz[/eluser]
Don't bother extending CI's model, just copy it to application/libraries and add your own functionality.
[eluser]taewoo[/eluser]
I have a similar issue. I am creating an abstract class that extends the CI model. My sub-models extend this abstract model (inheriting the methods) with different table name via the constructor Code: super('tableName') PHP is complaining that this generic, abstract model cannot be found (even though it's in the same directory) when a controller tries to load the extended sub-model. Is this a path issue or do I need to go wiredesignz method?
[eluser]treehousetim[/eluser]
At the risk of sounding like a noob (I'm not), and even though I have searched the CI code base and Googled for the function "super" you're calling - I'm clueless as to what it does. I think we might need more info about what you're trying to do. [quote author="taewoo" date="1215657378"]I have a similar issue. I am creating an abstract class that extends the CI model. My sub-models extend this abstract model (inheriting the methods) with different table name via the constructor Code: super('tableName') PHP is complaining that this generic, abstract model cannot be found (even though it's in the same directory) when a controller tries to load the extended sub-model. Is this a path issue or do I need to go wiredesignz method?[/quote]
[eluser]taewoo[/eluser]
Oh, oops. Maybe I meant "parent::Constructor()". This is what i'm trying to do Code: <?php And the sub-class Code: <?php
[eluser]roguewave[/eluser]
Did you figure out a way to pass the table name for a model?
[eluser]treehousetim[/eluser]
[quote author="roguewave" date="1218760150"]Did you figure out a way to pass the table name for a model?[/quote] I simply do this: Code: $this->tableName = 'mytable' in my constructor Full example: Code: class mytable extends MY_Model In practice my code is much more complex, but this is the basics. |
Welcome Guest, Not a member yet? Register Sign In |