Welcome Guest, Not a member yet? Register   Sign In
Is loading libraries within models a good idea?
#1

[eluser]sorenchr[/eluser]
I'm using a customly created MySQL input-sanitizer library in one of my controllers, which makes a mess, using it in the model would be much easier, but I don't wan't to defy any best-practice principles out there either. So is it a good idea?
#2

[eluser]InsiteFX[/eluser]
Why not use CodeIgniter's Input Class?

Input Class

And

Escaping Queries

Enjoy
InsiteFX
#3

[eluser]danmontgomery[/eluser]
[quote author="sorenchr" date="1269016515"]I'm using a customly created MySQL input-sanitizer library in one of my controllers, which makes a mess, using it in the model would be much easier, but I don't wan't to defy any best-practice principles out there either. So is it a good idea?[/quote]

The model has access to any libraries loaded in the controller when the model is loaded... So you could load it in the controller, then access it in the model.
#4

[eluser]frist44[/eluser]
The model has access to any libraries loaded in the controller when the model is loaded... So you could load it in the controller, then access it in the model.[/quote]

This is generally a bad idea because of the separation between the two pieces of code. What if the model is used in another controller, which is supposed to be one of the selling points of OOP. I use some of my models in 5-6 controllers, so I would ever want to have a model be dependent on something loaded from a controller.

I would first load it in the model if you need it from the model and nowhere else.
#5

[eluser]bretticus[/eluser]
The Model class calls one method: _assign_libraries().

The comments for this method are (bold added for emphasis):

Quote:* Creates local references to all currently instantiated objects
* so that any syntax that can be legally used in a controller
* can be used within models.

Just do it in the Model. No worries. You won't have any database overhead by choosing a Model either. $this->db is accessible in either a Controller or a Model.

Cheers!




Theme © iAndrew 2016 - Forum software by © MyBB