Models are optionally available for those who want to use a more traditional MVC approach.
- What is a Model?
- Anatomy of a Model
- Loading a Model
- Auto-Loading a Model
- Connecting to your Database
Models are irev scripts that are designed to work with information in your database. For example, let's say you use revIgniter to manage a blog. You might have a model script that contains handlers to insert, update, and retrieve your blog data. Here is an example of what such a model script might look like:
<?rev put gBASEPATH into gBASEPATH if gBASEPATH is "gBASEPATH" then put "No direct script access allowed." exit to top end if function getLastTenEntries put rigDbGet("entries", 10) into tQueryResult if tQueryResult["numrows"] > 0 then return tQueryResult end if return FALSE end getLastTenEntries command insertEntry put $_POST["title"] into tDataToInsert["title"] -- please read the note below put $_POST["content"] into tDataToInsert["content"] put the seconds into tDataToInsert["date"] get rigDbInsert("entries", tDataToInsert) return it end insertEntry command updateEntry put $_POST["title"] into tDataToUpdate["title"] put $_POST["content"] into tDataToUpdate["content"] put the seconds into tDataToUpdate["date"] put $_POST["id"] into tWhere["id"] get rigDbUpdate("entries", tDataToUpdate, tWhere) return it end updateEntry
Note: The handlers in the above example use the Active Record database handlers.
Note: For the sake of simplicity in this example we're using $_POST directly. This is generally bad practice, and a more common approach would be to use the Input Library rigVarPost("title").
Model scripts are stored in your application/models/ folder. They can be nested within sub-folders if you want this type of organization.
The basic prototype for a model script is this:
<?rev put gBASEPATH into gBASEPATH if gBASEPATH is "gBASEPATH" then put "No direct script access allowed." exit to top end if
Note: This is not mandatory! This is just for security reasons.
Your models will typically be loaded and called from within your controller handlers. To load a model you will use the following handler:
If your model is located in a sub-folder, include the relative path from your models folder. For example, if you have a model located at application/models/blog/queries.lc you'll load it using:
Here is an example of a controller, that loads a model, then serves a view:
<?rev # PUT YOUR HANDLER NAMES INTO THE GLOBAL gControllerHandlers AS A COMMA SEPARATED LIST put "blog" into gControllerHandlers command blog rigLoadModel "Blog" put getLastTenEntries() into gData["query"] get rigLoadView("blog") end blog
If you find that you need a particular model globally throughout your application, you can tell revIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload.lc file and adding the model to the autoload array.
When a model is loaded it does NOT connect automatically to your database. The following options for connecting are available to you:
- You can connect using the standard database handlers described here, either from within your Controller script or your Model script.
- You can tell the model loading handler to auto-connect by passing TRUE (boolean) via the second parameter,
and connectivity settings, as defined in your database config file will be used:
rigLoadModel "Model_name", TRUE
- You can manually pass database connectivity settings via the second parameter:
put "localhost" into tConfig["hostname"] put "myusername" into tConfig["username"] put "mypassword" into tConfig["password"] put "mydatabase" into tConfig["database"] put "mysql" into tConfig["dbdriver"] put "" into tConfig["dbprefix"] put FALSE into tConfig["pconnect"] put TRUE into tConfig["db_debug"] rigLoadModel "Model_name", tConfig