Welcome Guest, Not a member yet? Register   Sign In
Examples of using the Model class?
#17

[eluser]Chris Newton[/eluser]
The benefit of using models is that

1. you can use your database functions in multiple controllers (easily) Controllers shouldn't really call functions within other controllers, so you need a place to put your database calls that is not inside a controller (if you plan to have more than 1 controller).

2. You can load models into libraries. Libraries shouldn't load a controller, so it's better to have your database calls in the model. It's not a best practice to load a library or model in a library either (for portability purposes) so that point may be moot.

3. Models can't be called directly from the web, so it might aid your security setup. You can use _functionname in a Controller to limit external access to a function as well, so it's only a minor point.

If you choose to do so, the model can be doing something 'model-like' which is to store the functions used to control data flow into and out of the database, the business logic of the application. The developers of Rails have chosen to use ORM, which does not necessarily define the value of using a model in itself. Long before ORM, the MVC design pattern was outlined, and for years the M has been an abstraction that existed because it's handy to separate business logic from application function. In my experience it's been very useful to collect all of the database calls in a separate entity, for maintenance, multiple point connectivity, and portability. As you mentioned above, I've created functions within some of my models to auto-create tables if they're not found with an initialization function, which makes reusing some common-themed models a simple operation (like users models, etc)

To sum up. You don't HAVE to use the MVC pattern. The model's purpose is to contain business logic and interact with the database. ORM != Model though...


Messages In This Thread
Examples of using the Model class? - by El Forum - 07-08-2008, 01:08 AM
Examples of using the Model class? - by El Forum - 07-08-2008, 01:35 AM
Examples of using the Model class? - by El Forum - 07-08-2008, 02:06 AM
Examples of using the Model class? - by El Forum - 07-08-2008, 02:35 AM
Examples of using the Model class? - by El Forum - 07-08-2008, 02:44 AM
Examples of using the Model class? - by El Forum - 07-08-2008, 02:50 AM
Examples of using the Model class? - by El Forum - 07-08-2008, 03:04 AM
Examples of using the Model class? - by El Forum - 07-08-2008, 03:21 AM
Examples of using the Model class? - by El Forum - 07-08-2008, 05:24 PM
Examples of using the Model class? - by El Forum - 07-08-2008, 06:47 PM
Examples of using the Model class? - by El Forum - 07-08-2008, 06:53 PM
Examples of using the Model class? - by El Forum - 07-08-2008, 06:57 PM
Examples of using the Model class? - by El Forum - 07-08-2008, 07:03 PM
Examples of using the Model class? - by El Forum - 07-08-2008, 07:14 PM
Examples of using the Model class? - by El Forum - 07-09-2008, 12:37 AM
Examples of using the Model class? - by El Forum - 07-09-2008, 03:37 AM
Examples of using the Model class? - by El Forum - 07-09-2008, 04:53 AM
Examples of using the Model class? - by El Forum - 07-09-2008, 05:23 AM
Examples of using the Model class? - by El Forum - 07-09-2008, 08:08 PM
Examples of using the Model class? - by El Forum - 07-09-2008, 08:30 PM
Examples of using the Model class? - by El Forum - 07-09-2008, 08:40 PM
Examples of using the Model class? - by El Forum - 07-09-2008, 08:49 PM
Examples of using the Model class? - by El Forum - 07-09-2008, 09:22 PM
Examples of using the Model class? - by El Forum - 07-10-2008, 09:55 AM
Examples of using the Model class? - by El Forum - 07-10-2008, 05:16 PM
Examples of using the Model class? - by El Forum - 07-10-2008, 05:58 PM
Examples of using the Model class? - by El Forum - 07-10-2008, 08:45 PM
Examples of using the Model class? - by El Forum - 07-10-2008, 09:18 PM



Theme © iAndrew 2016 - Forum software by © MyBB