Welcome Guest, Not a member yet? Register   Sign In
Should I use models OR libraries?
#1

[eluser]IamPrototype[/eluser]
I want a file to contain all my "mini functions" like "gen_password", "send_email", "clear_login_attempts" etc. and I don't know if I should use a model or create a library? I know some CI CMS are using libraries, but eh.. I'm confused. Would it make a big difference? Smile Now if you say "model".. then uh-uh.. what can I use libraries for?
#2

[eluser]xwero[/eluser]
i think helper an plugin files are best suited for functions.
#3

[eluser]IamPrototype[/eluser]
Huh? I still don't get it. What code should a library and model contain? I know models is for queries etc., but.... eh...... libraries?
#4

[eluser]xwero[/eluser]
If you have a bunch of functions that depend on each other to achieve a goal then you have a library. Take a look at the CI libraries.

If the functions are stand alone for the most part put them in a helper.

if it's a stand alone function you can't categorize put it in a plugin.

From the function names you provided i would create a auth_helper for the gen_password and clear_login_attempts and a email plugin for the send_email function.
#5

[eluser]IamPrototype[/eluser]
So models is just... queries, right?
#6

[eluser]xwero[/eluser]
models are for data, data can come from a database, files, webservices.
#7

[eluser]IamPrototype[/eluser]
Ok, I'll use it like this:

Models -> Queries/MySQL/Database/Data (login, logout, edit, delete, create etc.)
Helpers -> Stand alone functions
Plug-in -> E-mail
#8

[eluser]junkwax[/eluser]
models are for business logic, not just queries ( data access ).

From wikipedia:

Model
Is the domain-specific representation of the information on which the application operates. Domain logic adds meaning to raw data (for example, calculating whether today is the user's birthday, or the totals, taxes, and shipping charges for shopping cart items).
Many applications use a persistent storage mechanism (such as a database) to store data. MVC does not specifically mention the data access layer because it is understood to be underneath or encapsulated by the model.


80% of my code is in models, 15% in controllers and 5% in views.

In CI, if you use libraries, you have to call get_instance() to access CI, so I use models with autoload so I can access all without calling get_instance();
#9

[eluser]Tom Schlick[/eluser]
all you have to do for the library for a global get_instance() is create a
Code:
var $CI;

__construct()
{
  $this->CI &= get_instance();
}

now just refer to $this->CI in the library and you are good to go. no need to lump everything in a disorganized mess in the model.
#10

[eluser]junkwax[/eluser]
my models are not "disorganized mess" =) I just do not see the benefit of libraries vs models for business logic. Accessing CI stuff with $this->CI->BLABLA is not very clean in my opinion si in modelsI can access CI stuff directly with $this->BLABLA

Maybe libraries are ok for external 3rd party libs, but even so, you cannot instantiate some of these without modifying their constructors because you can only call load->library with arrays as constructor args?




Theme © iAndrew 2016 - Forum software by © MyBB