Majere provides a web interface to create and manage your application's models. When you goto the Majere home screen you will see a listing of all of your installed applications. Click on the application that needs a model. This will take you to page with several headings, please turn your focus to the Models section. Click the 'Create new model' link'.
Creating a new model
There are three parts to this form.
- Name: Solstice automatically provides an application namespace and a model namespace for you, the rest is up to you.
- Description: This is used to get a head start in pod generation and is a required field.
- Base classes: This does just what it sounds like, defines a super class for your model. Solstice::Model provides a lot of rich functionality and for this example that will be the base class that we use.
After you have filled out the form click the save button. This will write out a file and create your model.
Using Accessor Definitions
One of the freebies every Solstice model gets is an accessor definition. Complicated models can have many attributes to them, and this is Solstice's answer to dealing with attributes. Any majere generated model will stub this method out for you, it will generate the following:
Perl
=item _getAccessorDefinition()
=cut
sub _getAccessorDefinition {
return [
];
}Each 'accessor' has several flags that can be set.
The three required fields are name, key, and type. There are three other optional fields that any accessor may have.
Perl
{
name => 'Name', # method name suffix: getName/setName in this example
key => '_name', # object key
type => 'String', # data type, see explanation below
taint => 0|1, # public set taints model, false by default
private_set => 0|1, # specifies that set is private, false by default
private_get => 0|1, # specifiies that get is private, false by default
},Solstice also enforces strict datatyping. If you set an accessor to be of type integer and it receives a floating point, Solstice will throw an exception and die.
To use these accessors in your code you would need to do something like this.
Perl
...
$model->setName('HelloWorld');
...
print $model->getName();You will notice that prepended to the name flag is the set/get string. If you had used the private_set/get flag you would need to use _setName and _getName respectively.
Managing them in Majere
Majere provides an automatic way to create and manage your models attributes. From the manage model page simply click on the model you wish to work. The subsequent screen will show you any accessors your model currently has, and also give you the ability to create and delete accessors.
To create a new accessor fill in the Name, and Datatype field, the other checkboxes on that form are optional and for an explanation of all of the fields can be seen found in the section above. After you are satified with the name and datatype click save changes, or if you wish to add another accessor click the add button.
Viewing your model's methods
A neat little feature provided by Majere is the ability to see the code of any method within your model. Below the accessor definition section is a list of all of your methods. If you wish to see the code generated by Majere just click the view button.
Direct Editing
Often times a as a programmer you will be deep into filling out your controllers and views and will not want to have to fire up a web browser to edit the taint flag in Majere. Majere stubs out the accessor defintions at the bottom of the model, so open up your model file, go near the bottom and code away.
Initialization and Storage
How and why to do minimal initialization
Using Solstice::Database
Models in the contoller lifecycle
The controller is where user input has been collected, and needs to be stored into the model. Different tasks, updating the model, validating, storing, etc, might need to be done to the model. There are specific functions that take on these roles and preform these tasks.
what happens at each stage
The five methods the controller runs are freshen, update, validate, commit and revert. They will also be run in this order.
Freshen
This method is used to get the model in a prestine state. The model could be stale and have data that is old, or no longer accurate. This is the programmers chance to get a new clean copy of the model out of storage.
Update
Update pulls out user data and updates the model with it. Solstice uses persistence values to update all of our models. For more on persistence please see below.
Validate
The user data is validated.
Commit
Values are pulled from persistence put into the real model accessors. The model is then stored, and any logging and messaging is done here.
Revert
Revert allows the programmer to undo any changes. If you have a model persistening data from click to click, and you want to clean it all out if they click cancel, for example, revert would be the place to clear those changes.
Using Persistence Values
From a application writer's perspective it is adventagous to be able to display the users invalid data on the form after an attempted submission. Also of use, is helpful messaging. This allows the user to figure out why his data was not accepted. This provides a slight problem because all solstice models are strictly typed. This means you cannot put bad data directly into your model. While this is great for validation and many other things it does present us with a small challenge. Solstice provides a way to temporarily store unvalidated data into the model so that it may persist from click to click until it has been succesfully validated and stored into the model.
As an application writer using solstice it is recommended to use persistence in your update method and then retrieve those values in commit and store them into your data model. IE.
Perl
sub update {
my $self = shift;
my $model = $self->getModel();
$model->setPersistenceInteger(param('my_integer'));
}In update we blindly set the form param into persistence we would then need to run validate and ensure that the user did indeed input an integer. Assuming validate did indeed pass we would want to run the following code in commit:
Perl
sub commit {
my $self = shift;
my $model = $self->getModel();
#you can do this one of 2 ways
$model->processPersistenceValues();
#or you can do it manaully for each persistence value
$model->setInteger($model->getPersistenceInteger());
}The processPersistenceValues method finds each persistence value and puts it into the correct model accessor. If you have a model with a large number of accessors this can make your commit method much shorter and easier to write.
Clearing Persistence Values
Solstice::Model also provides a way to clear all persistence values from the model. Calling clearPersistenceValues() on the model will clear out all persisted values.