Welcome Guest, Not a member yet? Register   Sign In
Is good practice to load libraries inside models?
#1

[eluser]Cesar Kohl[/eluser]
I know it does works, but is it good practice?
#2

[eluser]n0xie[/eluser]
No.
#3

[eluser]WanWizard[/eluser]
I don't entirely agree.

I can imagine situations where you would have generic validation or data transformation methods, it would be logical and more portable to store those in a library (then for example in a My_Model).
#4

[eluser]Cesar Kohl[/eluser]
So, it depends. That's interesting.
#5

[eluser]n0xie[/eluser]
Ok maybe I should elaborate. It's not good practice in general. There are of course exceptions. The trouble with using libraries in models is that it creates a tight coupling between the model and the library. This should, by definition, be avoided.
#6

[eluser]WanWizard[/eluser]
Ok, fair enough.

Just curious: where would you leave the type of methods I mentioned? In MY_Model? Even if they are substantial in size and not every model needs them?
#7

[eluser]techgnome[/eluser]
n0xie - yes and no... I guess it depends on how you structure it. I've seen people here advocate the putting of the biz logic into the model... leaving the controllers stupid drones that do nothing more than call a model, load a view and pass the data.

That said... I don't subscribe to that model at all. I load all my libraries and helpers in the controller, call the model, get the data, perform my BL and pass it on to the view. This makes my controllers heavy, but that's how it should be. The controller is the brains of the bunch.

-tg
#8

[eluser]n0xie[/eluser]
[quote author="WanWizard" date="1292391485"]Ok, fair enough.

Just curious: where would you leave the type of methods I mentioned? In MY_Model? Even if they are substantial in size and not every model needs them?[/quote]
That is one of the toughest cookies to crack. Do I sanitize/validate inside the controller or inside the model? It's something I don't have a good answer for as of yet.

Personally I do all the validating and sanitizing inside the controller and keep the model 'dumb'. That's not adhering to thin controllers/fat models and actually breaks MVC (since the model should be application/data business logic), but this way I don't have to change both the controller and the model every time another field gets added to the form.

When doing the validating/ sanitizing inside your model, I would either put them in a base class or maybe pass a validation object as a parameter. If it would be cleaner to just load an library, I would just as easily do that, but I would at least consider the other options. The trouble is, that if you start this train of thought, you might as well add your 'security library' to the mix as well. After all it's 'just' data and it needs to be cleaned. Then maybe load an helper to format an array a specific way. The next step is that you just grab the POST array straight from your model and sooner or later you'll be in code spaghetti hell.

It's a slippery slope and you should be wary of it. Try to keep pieces of code as decoupled as possible without breaking DRY.

tl;dr: use whatever you think is appropriate :-P
#9

[eluser]wiredesignz[/eluser]
Put validation field names, labels, rules and messages into a config file. Load it in the controller and it will be available to your models too. The you can sanitize in the controller and validate in the model or whatever is required.

Only 1 file to update.
#10

[eluser]tonanbarbarian[/eluser]
there is one library that is commonly used in the model, while not always loaded in the model it can be
and that of course is the database library

IF the library is useful in gathering or processing the data your model represents then you should load it in the model and not in the controller, because the controller should not have to know that model x required library y to operate

since models interact with data then the libraries that you load in the model should facilitate that.
for example if you need to read some information from an xml file or xls file you might have a model that then loads the necessary xml or xls library to be able to read the data.

since the idea behind MVC is abstraction, and the controller should not need to know how the model interacts with its data then loading necessary libraries in models should be fine




Theme © iAndrew 2016 - Forum software by © MyBB