Welcome Guest, Not a member yet? Register   Sign In
How to create reusable model or controller ?
#1

[eluser]Wades[/eluser]
Hi guys, after using CI for developing several websites, I really love this framework.
and now I'd like to make it more flexible, so I want to make some reusable models or controller. What I want to do is that:

suppose there are two models: A_model and B_model
then there is a function in A_model called function_a();
so how could I use function_a() of A_model in B_model?
Is anybody can help me?
#2

[eluser]Référencement Google[/eluser]
Why not that B extends the A model? Not quiet sure we can extend models with others but give it a try.
#3

[eluser]imzyos[/eluser]
extends... mmm, all Models must extends CI_Model, helper, or your own library.
#4

[eluser]Wades[/eluser]
thanks elitemedia, I've tried this way, yes, B can extends A model,
but if there is another base model C, then B also wants to use the
functions in C, then is there any way to get this work?
I want to make it something like java, when you want to use a class,
then just import it and then it can be used.
anyone who has this kind of experience in CI?
thank you!
#5

[eluser]Luci3n[/eluser]
PHP5 can only extend one class, you will need to create a generic class which extends the the base model class then after extend the generic class See this thread
#6

[eluser]xwero[/eluser]
A workaround solution is to create a library where you define functions you want to share.
Code:
class modelfunctions{
    function modelA_function1()
    {
        $this->load->model('modelA','',true);
        return $this->modelA->function1();
    }
     function modelB_function5()
    {
        $this->load->model('modelB','',true);
        return $this->modelB->function3();
    }
}
If you autoload that library you can use those functions you defined in any model you want.
I like this way better than extending models because you have more loose coupling between the models.
#7

[eluser]Wades[/eluser]
wow, tks a million xwero, this is really an great idea
#8

[eluser]Référencement Google[/eluser]
xwero this is very smart! Thanks for sharing this idea.
#9

[eluser]xwero[/eluser]
no problem i get many ideas from looking at other ones solutions so sharing comes natural.




Theme © iAndrew 2016 - Forum software by © MyBB