Models

Models are optionally available for those who want to use a more traditional MVC approach.

What is a Model?

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").

Anatomy of a Model

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.

Loading a Model

Your models will typically be loaded and called from within your controller handlers. To load a model you will use the following handler:

rigLoadModel "modelName"

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:

rigLoadModel "blog/queries"

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

Auto-loading Models

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.

Connecting to your Database

When a model is loaded it does NOT connect automatically to your database. The following options for connecting are available to you: