Phinx Documentation, Release 0.8.1
You can then manipulate this table using the methods provided by the Table object.
The Save Method
When working with the Table object, Phinx stores certain operations in a pending changes cache.
When in doubt, it is recommended you call this method. It will commit any pending changes to the database.
Creating a Table
Creating a table is really easy using the Table object. Let’s create a table to store a collection of users.
<?php
use Phinx\Migration\AbstractMigration;
class MyNewMigration extends AbstractMigration
{
/
**
*
Migrate Up.
*
/
public function up()
{
$users = $this->table('users');
$users->addColumn('username', 'string', array('limit' => 20))
->addColumn('password', 'string', array('limit' => 40))
->addColumn('password_salt', 'string', array('limit' => 40))
->addColumn('email', 'string', array('limit' => 100))
->addColumn('first_name', 'string', array('limit' => 30))
->addColumn('last_name', 'string', array('limit' => 30))
->addColumn('created', 'datetime')
->addColumn('updated', 'datetime', array('null' => true))
->addIndex(array('username', 'email'), array('unique' => true))
->save();
}
/
**
*
Migrate Down.
*
/
public function down()
{
}
}
Columns are added using the addColumn() method. We create a unique index for both the username and email
columns using the addIndex() method. Finally calling save() commits the changes to the database.
Note: Phinx automatically creates an auto-incrementing primary key column called id for every table.
The id option sets the name of the automatically created identity field, while the primary_key option selects the
field or fields used for primary key. The primary_key option always defaults to the value of id. Both can be
disabled by setting them to false.
To specify an alternate primary key, you can specify the primary_key option when accessing the Table object.
Let’s disable the automatic id column and create a primary key using two columns instead:
10 Chapter 1. Contents