Welcome Guest, Not a member yet? Register   Sign In
Using common functions in multiple models
#1

[eluser]veliscorin[/eluser]
Hi guys,

I currently have several models, each with similar functions like so:
Code:
user_model->get_user_by_id($id);
book_model->get_book_by_id($id);
receipt_model->get_receipt_by_id($id);
Since they are all similar functions and return similar data, I wish to do it in such a way that I do not have to repeat these code in subsequent newer models that I create. I was thinking of doing it such a way that I shall have something like this:
Code:
some_generic_name->get_single_row($table_name, $id);
So that in my different controllers, I could use something like this:
Code:
$user = $this->some_generic_name->get_single_row('tbl_users', '1');
//or
$book = $this->some_generic_name->get_single_row('tbl_books', '1');
Is there a way to do this? Or am I not going in the direction that things are supposed to be done...

Many thanks in advance to the community =)
#2

[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.
#3

[eluser]tonanbarbarian[/eluser]
For information about extending the Model class see
Replacing Native Libraries with Your Versions
#4

[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 {
    function MY_Model()
    {
        parent::Model();
    }

    function test()
    {
        return 'testing';
    }  
}
However it is not working when i try to run one of my models $this->user_model->test();
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?
#5

[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?
#6

[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
#7

[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();
#8

[eluser]tonanbarbarian[/eluser]
should be fine but if you post your actual code i can confirm what you are doing wrong
#9

[eluser]Grahack[/eluser]
In this case, do we have to code
Code:
class Test extends MY_Model { ...
Code:
class Test extends Model { ...
My guess is #1, but maybe I'm wrong. Which one did you choose veliscorin?
#10

[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 { ...
My guess is #1, but maybe I'm wrong. Which one did you choose veliscorin?[/quote]
i chose #2




Theme © iAndrew 2016 - Forum software by © MyBB