Make a Model From a Database Table
At this point, you have downloaded the Solar system, made a vendor
space to work in, and configured the system. Next, we will create
a database table and make a model to map to it.
Create a table called posts in your database.
If you create an SQLite database, make sure to chmod
it so that the web server has read and write privileges.
At the command line, in the SYSTEM directory,
issue the make-model command. You will see something
like the following:
$ ./script/solar make-model Acme_Model_Posts
Making model 'Acme_Model_Posts'.
Will write to 'SYSTEM/include/'.
Will connect to database for column information.
Using table 'posts'.
Not using inheritance.
Making class directory ... done.
Writing model class ... done.
Writing record class ... done.
Writing collection class ... done.
Creating setup directory ... Done.
Saving table name for setup ... done.
Creating empty table cols setup ... done.
Connecting to database ... connected.
Fetching table cols ... done.
Saving table cols for setup ... done.
Creating empty index info setup ... done.
Connecting to database ... connected.
Fetching index info ... no indexes found.
Creating locale directory ... done.
Saving locale file for en_US ... done.
Done.
What happens here is that Solar looks at the class name passed
to make-model, takes the part after the
last underscore, and uses that as the table name. It then
looks up that table in the database and creates files for the
model definition, record class, collection class, column and
index setup information, and locale strings.
Solar models are smart enough to recognize basic validation
constraints based on the column definitions (e.g., NOT NULL
fields must be not-empty before being saved). However, we
usually need additional validation on model data. Let's make
it so that the status column must be one of
a list of values.
Open
SYSTEM/source/acme/Acme/Model/Posts.php
and edit the _setup() method to add a
filter on the status element.
_addFilter('status', 'validateInList', array(
'draft',
'public',
));
}
]]>
There are lots of filters available to validate and sanitize
data. You can examine the
SYSTEM/source/solar/Solar/Filter
directory for a full list of them.