Overview
This entry is a continuation of the Solar CLI series--a series that aims to detail Solar CLI commands, available options, parameters, and usage examples. In this entry we take a look at make-model, a command that can generate models based on a SQL table.
First Glance
Make-model reduces the time it takes to set up models, which can be very time consuming to setup by hand. Make-model streamlines this process by reading your existing SQL table and creating the model based off of this information.
Let's first start by taking a look at what help can tell us about the command make-vendor.
Note: It is assumed that your Solar System is installed under /var/www/solar/.
If you have not done so already, change directories to the root of your Solar System. In our case, that would be /var/www/solar/.
Help Information
$ ./script/solar help make-model
Using include_path '.:..:/var/www/solar/include'.
Using config file '/var/www/solar/config/Solar.config.php'.
No help is available for this command.
Valid options for this command are...
--adapter
: The SQL adapter class to use.
--config
: Use this configuration file when starting Solar.
--connect
: Connect to the database and fetch cols for the model setup.
--extends
: Extend the model class from this parent class name.
--host
: The host for the database connection.
--name
: The name of the database to connect to.
--pass
: The password for the database connection.
--port
: The port for the database connection.
--table
: The table name for the model to use.
--target
: Write files to this directory, typically the include directory.
--user
: The username for the database connection.
-V | --version
: Display version information and exit.
-v | --verbose
: Display verbose output when available.
Available Options
Adapter The SQL adapter to use. Examples include Solar_Sql_Adapter_Mysql and Solar_Sql_Adapter_Sqlite.
Connect: If set to true, Solar connects to the SQL table to generate the model to reflect the columns in the table.
Extends: The name of the class that we want to extend. Defaults to Solar_Sql_Model.
Host: The host where the database resides (e.g. localhost).
Name: Name of the database to connect to.
Pass: Password to be used with user name.
Port: Port that our database is running on.
Table: Table name we want to create our model for and from.
User: User name that has access to the database table.
Available Parameters
Class: The class name. This is required. An example would be Vendor_Model_Posts.
Usage Examples
Now, this is where using a configuration file makes life easy. First though, I am going to show you the (more time consuming) way to generate a model without changing your current (default) configuration file.
Just so I do not lose you, let me specify the basic settings I am using for my database.
- Database type: MySQL
- Database name: vendor
- Host: localhost
- User name: root
- Password: s3cr3tP@ssw0rd
Now let's generate a model that controls blog posts. The name of this model will be posts. The table layout we will be using looks like this:
- id (primary key, auto increment)
- title (varchar(100))
- body (text)
- author (varchar(100))
- created (datetime)
- updated (datetime)
For the lazy people reading this, you can just run the following MySQL to generate the table.
CREATE TABLE posts(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
title VARCHAR(100),
body VARCHAR(100),
author VARCHAR(100),
created DATETIME,
updated DATETIME
) engine=INNODB
Now on to generating our model for posts (the long way).
$ ./source/solar make-model --adapter=Solar_Sql_Adapter_Mysql --connect=true --host=localhost --name=vendor --pass=s3cr3tP@ssw0rd --table=posts --user=root Vendor_Model_Posts
That is all there is to it. You can see the files generated by changing directories to /var/www/solar/source/vendor/Vendor/Model/. Here you will see the following layout.
- /Posts.php
- /Posts/Collection.php
- /Posts/Record.php
- /Posts/Setup
- /Posts/Setup/table_cols.php
- /Posts/Setup/table_name.php
Now, the easy way to generate models is to save your database connection information to your configuration file. Go ahead and open up Solar.config.php, located under the directory /var/www/solar/config/.
Add the following to the configuration file.
<?php
$config['Solar_Sql']['adapter'] = 'Solar_Sql_Adapter_Mysql';
$config['Solar_Sql_Adapter_Mysql'] = array(
'host' = 'localhost',
'user' = 'root',
'pass' = 's3cr3tP@ssw0rd',
'name' = 'vendor',
);
?>
Now, when we want to generate a model, we only need to do the following.
$ ./script/solar make-model --table=posts Vendor_Model_Posts
What's Next?
In my next post I will cover make-app, a command used to auto-generate application skeletons.
Until then, join the Solar mailing list and/or IRC channel -- we have cookies.
Comments
No comments