CLI (command-line interface)
CLI is the command-line interface included with Crater PHP Framework. It provides a number of helpful commands for your use while developing your application.
Listing all available commands
To view a list of all available CLI category commands, you may use command:
$ cd /var/www/crater/App $ php cli
BUILD command
With build command, you can generate a new controller, model, view or template
For help menu, type:
$ php cli build
MIGRATE command
With migrate command, you can create and manage the migration files
First of all, you need to change config file for access at your database. After this, you need to init Crater migration with following command:
$ php cli migrate init
In the next step, you need generate a new migration file:
$ php cli migrate new name_of_migration
After that, you will find a new file in App/Data/Migrations called 1428642875_name_of_migration.php
The created class will have the timestamp of its creation in the class name as well as in the file name. This is the version of that migration.
The migration file looks like:
/* * name_of_migration migration file * * @author Author * @version 1.0 * @date 4/20/2015 */ class Migration_1428642875 extends \Core\Migration { public function up() { } public function down() { } }
Migration methods
The following methods are available in up() and down() methods of a migration:
- createTable($name, $columns)
- dropTable($name)
- addColumn($table, $name, $options)
- dropColumn($table, $name)
- addIndex($table, $columns)
- dropIndex($table, $indexName)
- query($sql)
Please view examples heading on how to use the above methods.
Apply Migration
After you have created a migration, all you have to do to update your database is run:
$ php cli migrate apply
Rollback Migration
To rollback the database schema you run the following command:
$ php cli migrate rollback [version]
where [version] is the timestamp of the version you want to reach to back in time.
Examples
class Migration_1428642851 extends \Core\Migration { public function up() { $this->createTable('user', array( 'user_id' => array('type' => 'int', 'unsigned' => true, 'primary' => true, 'ai' => true), 'first_name' => array('type' => 'varchar', 'length' => 60, 'null' => true), 'last_name' => array('type' => 'varchar', 'length' => 60, 'null' => true), 'email' => array('type' => 'varchar', 'length' => 40, 'null' => false), )); $this->addColumn('product', 'name', array( 'type' => 'varchar', 'after' => 'id' )); $this->addIndex('user', 'email', 'unique'); $this->query("UPDATE user SET name = 'john' WHERE name = 'johnny'"); } public function down() { $this->dropColumn('product', 'name'); $this->dropTable('user'); } }↑