Welcome Guest, Not a member yet? Register   Sign In
Autoload a model and run a function?
#1

[eluser]chefnelone[/eluser]
Hello,

Is there any way to autoload a model and autorun a function (which is inside that model)

I have all the texts of my site storaged in a database, then regardless which page of the site is loading I need to recover those texts from the data base.

I think the best way to do this is to autoload a model run a function and fetch all the texts. Then I'd save them in a Session variable to access it whenever I need it.

I know I can autoload a model but not how to autorun a function, if possible.
If not, what can I do?
#2

[eluser]helmutbjorg[/eluser]
You could execute the function in the constructor of the model like so:

Code:
class Example extends Model {

    function Example() {
        parent::Model();
        $this->somefunction();
    }
    
    function somefunction() {
        // Do something here...
    }

}

That function would be called every time the class is loaded.
#3

[eluser]John Pantoja[/eluser]
How much memory do you have on your server? If you did that per user? Might be easier to use the database cache or just have something unique passed to your controller (index() or _remap()) to retrieve for you...

WHat's the reasoning behind this, maybe since it's bed time I'm not feeling your steelo
#4

[eluser]John Pantoja[/eluser]
[quote author="helmutbjorg" date="1264082653"]You could execute the function in the constructor of the model like so:

Code:
class Example extends Model {

    function Example() {
        parent::Model();
        $this->somefunction();
    }
    
    function somefunction() {
        // Do something here...
    }

}

That function would be called every time the class is loaded.[/quote]

Actually I kind of like that idea, nifty.
#5

[eluser]chefnelone[/eluser]
[quote author="helmutbjorg" date="1264082653"]You could execute the function in the constructor of the model like so:

Code:
class Example extends Model {

    function Example() {
        parent::Model();
        $this->somefunction();
    }
    
    function somefunction() {
        // Do something here...
    }

}

That function would be called every time the class is loaded.[/quote]

This is the way I've done it. But I need to call 'somefunction' in all controllers.
Is there a way to autoload the 'Example' model and autorun 'somefunction'?
#6

[eluser]flaky[/eluser]
http://ellislab.com/codeigniter/user-gui...oader.html
#7

[eluser]tomcode[/eluser]
Other possibilities :

1. You can make a hook
2. You can work with a MY_Controller
#8

[eluser]chefnelone[/eluser]
[quote author="tomcode" date="1264085775"]Other possibilities :

1. You can make a hook
2. You can work with a MY_Controller[/quote]

thanks

I think a hook can make the work.

I'm just wondering if using hooks Will this affect the loading time for the page.
#9

[eluser]tomcode[/eluser]
Sure, every file load adds a little as any additional code or db query.
#10

[eluser]rogierb[/eluser]
Everytime you load something your script gets slower, so yes using hooks slows down your script. Just as loading a view, model or library.

But to sooth you mind, the hooks class is always loaded and the hooks are always checked. So you just have to worry about the hook itself.

As to you original question, I would go for a MY_Controller. That seems like the right place to put functionality that is needed for each controller like text, templates etc.

Hooks are generally used to extend core funtionality.




Theme © iAndrew 2016 - Forum software by © MyBB