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 script only stacks 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:
script "blog" global gRigA on libraryStack if (gRigA is not an array) and (the environment is "server") then put "No direct script access allowed." exit to top end if if the short name of the target <> the short name of me then pass libraryStack end if end libraryStack 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:
script "Model_name" global gRigA on libraryStack if (gRigA is not an array) and (the environment is "server") then put "No direct script access allowed." exit to top end if if the short name of the target <> the short name of me then pass libraryStack end if end libraryStack
Note: The libraryStack handler as shown above is not mandatory! It is used just for security reasons.
Note: All revIgniter script only stack files use "livecodescript" as file extension. This convention you should keep in mind when you build models, libraries, helpers etc.
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:
<?lc # 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 only stack.
- 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