Using common functions in multiple models |
[eluser]veliscorin[/eluser]
Hi guys, I currently have several models, each with similar functions like so: Code: user_model->get_user_by_id($id); Code: some_generic_name->get_single_row($table_name, $id); Code: $user = $this->some_generic_name->get_single_row('tbl_users', '1'); Many thanks in advance to the community =)
[eluser]sophistry[/eluser]
hi veliscorin and welcome to CI... the standard Object Oriented Programming (OOP) way to do this is to extend the model class by subclassing it and put your shared code in the parent class. there are also several Object Relational Mapping (ORM) approaches (one simple one posted this week in the forums) that are more comprehensive but more complex. good luck and let us know how you fare.
[eluser]tonanbarbarian[/eluser]
For information about extending the Model class see Replacing Native Libraries with Your Versions
[eluser]veliscorin[/eluser]
Is it a good method to replace the entire model library with my own? Or is it more advisable to extend it? Currently my code runs as this, in application/library/MY_Model.php: Code: class MY_Model extends Model { When I try to extend some other libraries like, validation for example, I can successfully run $this->validation->test(). Am I doing something wrong here?
[eluser]veliscorin[/eluser]
[quote author="sophistry" date="1202430149"]hi veliscorin and welcome to CI... the standard Object Oriented Programming (OOP) way to do this is to extend the model class by subclassing it and put your shared code in the parent class. there are also several Object Relational Mapping (ORM) approaches (one simple one posted this week in the forums) that are more comprehensive but more complex. good luck and let us know how you fare.[/quote] Hi Sophistry, I found this ORM approach called Ezmodel in another post. The author extends the core Model and names it Ezmodel. Then in every subsequent model he creates in his application, it extends the Ezmodel class. The standard OO way is to extend the core model, and subsequently, all application models also should extend the core model right?
[eluser]tonanbarbarian[/eluser]
extending the model as you indicated in your example is always the way to go. if you completely replace rather then extending you run the risk of breaking some existing functionality
[eluser]veliscorin[/eluser]
Hi tonanbarbarian, In the example that I have given, is there something wrong that I am doing? I can't seem to get it to work. Is the usage even correct? ie: $this->user_model->test();
[eluser]tonanbarbarian[/eluser]
should be fine but if you post your actual code i can confirm what you are doing wrong
[eluser]Grahack[/eluser]
In this case, do we have to code Code: class Test extends MY_Model { ... Code: class Test extends Model { ...
[eluser]veliscorin[/eluser]
[quote author="Grahack" date="1202474652"]In this case, do we have to code Code: class Test extends MY_Model { ... Code: class Test extends Model { ... i chose #2 |
Welcome Guest, Not a member yet? Register Sign In |