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
blogs
in your database.-- SQLite CREATE TABLE blogs ( id INTEGER PRIMARY KEY AUTOINCREMENT, created TIMESTAMP DEFAULT NULL, updated TIMESTAMP DEFAULT NULL, status VARCHAR(15), title VARCHAR(63) NOT NULL, body CLOB ); -- MySQL CREATE TABLE blogs ( id INTEGER AUTO_INCREMENT PRIMARY KEY, created DATETIME DEFAULT NULL, updated DATETIME DEFAULT NULL, status VARCHAR(15), title VARCHAR(63) NOT NULL, body TEXT ); -- PostgreSQL CREATE TABLE blogs ( id SERIAL PRIMARY KEY, created TIMESTAMP DEFAULT NULL, updated TIMESTAMP DEFAULT NULL, status VARCHAR(15), title VARCHAR(63) NOT NULL, body LONGTEXT );
Note If you create an SQLite database, make sure to chmod it so that the web server has read and write privileges.
-
Now that we have a table, let's add some indexes and bare-bones data to it so we have something to look at later.
INSERT INTO blogs (created, updated, status, title, body) VALUES ( '2010-01-01 00:00:00', '2010-01-01 00:00:00', 'public', 'Public Blog Entry', 'First post!' ); INSERT INTO blogs (created, updated, status, title, body) VALUES ( '2010-01-01 06:00:00', '2010-01-01 06:00:00', 'draft', 'A Draft Article', 'My, it sure is drafty in here.' ); INSERT INTO blogs (created, updated, status, title, body) VALUES ( '2010-01-01 12:00:00', '2010-01-01 12:00:00', 'draft', 'Please delete me', 'This is here to be deleted.' );
-
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_Blogs
Making model 'Acme_Model_Blogs'. Will write to 'SYSTEM
/include/'. Making model 'Acme_Model_Blogs'. Will write to '/Users/pmjones/Sites/blog-demo/include/'. Using table 'blogs'. Not using inheritance. Making class directory ... done. Writing model class ... done. Writing record class ... done. Writing collection class ... done. Connecting to database for metadata ...connected. Fetching table cols ... done. Fetching index info ... no indexes found. Writing metadata class ... done. Creating locale directory ... done. Saving locale file for en_US ... done. $Note 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 not be blank 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
and edit theSYSTEM
/source/acme/Acme/Model/Blogs.php_setup()
method to add a filter on thestatus
element.<?php protected function _setup() { // chain to parent parent::_setup(); // add a validation filter on the status column $this->_addFilter('status', 'validateInList', array( 'draft', 'public', )); }
Note There are lots of filters available to validate and sanitize data. See the Solar_Filter package for a full list.