Class Misago\ActiveRecord\Table

Helper to create new tables. Generally used in migrations, but permits to create temporary tables on the fly too.

$t = $this->connection->new_table('authors');
$t->add_column('name', 'string', array('limit' => 50));
$t->add_timestamps('date');
$t->create(); 

Create a temporary table, with no primary key:

$t = $this->connection->new_table('stats', array('primary_key' => false, 'temporary' => true));
$t->add_column('counts', 'integer');
$t->create(); 

Public instance attributes

Protected instance attributes

Methods

Public instance methods

__construct($name, array $options=null, ConnectionAdapters\AbstractAdapter $connection)

Constructor.

Options:

  • id - true to automatically create the primary key column (default)
  • options - addition to table creation (eg: 'engine=myisam')
  • temporary - true to create a temporary table
  • force - true to drop table before create, otherwise creates if not exists

add_column($column, $type, array $options=null)

Adds a column to table's definition.

Types:

  • primary_key (auto_increment integer, serial, ...)
  • string
  • text
  • integer
  • float
  • decimal
  • date
  • time
  • datetime
  • boolean
  • binary

Options:

  • primary_key - true if column is the primary key
  • null - can the column be null? (default: true)
  • limit - maximum size
  • default - default value
  • signed - is the integer/float column signed?

Examples:

$t->add_column('name',  'string',  array('limit' => 50));
$t->add_column('price', 'numeric'); 

add_timestamps($type='datetime')

Adds timestamp columns to table's definition.

Types:

  • date - will add created_on & updated_on.
  • time - will add created_at & updated_at.
  • datetime - will add created_at & updated_at.

create()

Actually creates the table in database.