Solar provides a command line interface for generating models from existing database tables. This tutorial will show how to do this, as well as how to automate the process for several tables or an entire database.
Solar provides automatic transparent table generation from model classes, however some times it is necessary to generate models from tables. For example, one such circumstance is legacy databases.
Solar comes with a command line tool named “solar”. This tool provides some very useful commands. On a typical Ubuntu linux system, you can execute this tool like this:
$ php /usr/share/php/bin/solar
Which will display a short help message. To see a list of available commands, type:
$ php /usr/share/php/bin/solar help
The basic syntax for generating a model class is as follows:
$ php /usr/share/php/bin/solar make-model [options] class_name
For the sake of simplicity, it is benefitial to symlink the solar script to your /usr/bin directory to allow executing the script directly simply by typing solar on the command line. Here's how:
$ sudo ln -s /usr/share/php/bin/solar /usr/bin/solar
And that's it! Let's see what happens if we type solar on the command line:
$ solar Not using a config file. Solar command-line tool. Usage: solar <command> <options> <params> Try 'solar help' for a list of commands.
Excellent! Now we're all set.
To create a model from a database table, we need to configure the command line tool. This can be done by adding options. To see a complete list of options, type:
$ solar help make-model
In our case, we need to specify the adapter, the host name, the database name, as well as username and password. Additionally, we need to specify an output directory:
$ solar make-model --adapter Solar_Sql_Adapter_Mysql --host localhost --name bookmarksdb --user root --pass beer --table bookmark --target ~/projects/MyBookmarks Vendor_Model_Bookmark
Note: At the time of writing, there's a bug in the solar make-model command that prevents the model class file from being written to disk. If you create the MyBookmarks/Vendor/Model folder first, the problem goes away. This bug has been reported to Paul M.
TODO: configuration files
Let's create our first model class! Open up your favorite MySQL administration tool (I prefer http://phpmyadmin.net) and create a database called “bookmarksdb” with the following table:
CREATE TABLE `bookmark` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `link` VARCHAR( 255 ) NOT NULL , `title` VARCHAR( 255 ) NOT NULL , `description` TEXT NOT NULL , `date_entered` DATETIME NOT NULL ) ENGINE = innodb;
Now cd to your favorite project directory and create a new directory for our custom lightweight bookmark project, for example MyBookmarks.
Now you're all set to generate your first model class! cd back to your home directory and repeat the command above, assuming a username root with password beer (which is a bad password, by the way). You can swap the name Vendor with your own vendor name:
$ solar make-model --adapter Solar_Sql_Adapter_Mysql --host localhost --name bookmarksdb --user root --pass beer --table bookmark --target ~/projects/MyBookmarks Vendor_Model_Bookmark Not using a config file. Making model. Using table 'bookmark'. Writing 'Vendor_Model_Bookmark' to '/home/andreas/projects/MyBookmarks/'. Writing model class. Updating table cols. Updating table name. Writing record class. Writing collection class. Done.
This creates the following files and folders:
andreas@slappy:~$ tree projects/MyBookmarks/
projects/MyBookmarks/
`-- Vendor
`-- Model
|-- Bookmark
| |-- Collection.php
| |-- Record.php
| `-- Setup
| |-- table_cols.php
| `-- table_name.php
`-- Bookmark.php
4 directories, 5 files
Notice the familiar directory structure. As you would expect, the Bookmark.php file contains the class Vendor_Model_Bookmark.
TODO: Describe the various files
Now you are ready to use the bookmark model in your application. However, our quest is not yet over.
For large legacy databases, it would be rather tedious to manually generate a class model for each and every table. So to scratch that itch, we need to automate the whole process.
First off, we need a way to get a list of all the database tables. Here's one way to do it:
echo use bookmarksdb\;show tables\; |mysql -u root -p|grep -v Tables_in_
Geez!!