Database Class Methods

create() method

Purpose:
The create() method tells the database class that you want to create a new table/collection.

Format:

create(new_table_name)

Examples:

// Create a table/collection named 'locations'
$db ->create('locations')
    ->field('id', 'int', 'NOT NULL AUTO_INCREMENT')
    ->field('name', 'varchar(255)')
    ->field('address', 'varchar(255)')
    ->field('user_id', 'int')
    ->primaryKey('id')
    ->foreignKey('user_id', 'users', 'id')
    ->exec();

field() method

Purpose:
The field() method denotes a column you want to create on a table.

Format:

field(column_name, column_type, special_attributes)

Examples:

// Create a table/collection named 'locations'
// Create id, name, address, and user_id columns on it.
$db ->create('locations')
    ->field('id', 'int', 'NOT NULL AUTO_INCREMENT')
    ->field('name', 'varchar(255)')
    ->field('address', 'varchar(255)')
    ->field('user_id', 'int')
    ->primaryKey('id')
    ->foreignKey('user_id', 'users', 'id')
    ->exec();

primaryKey() method

Purpose:
The primaryKey() method tells the database class you want to create a primary key index on a column if the underlying database supports it.

Format:

primaryKey(column_name)

Examples:

// Create a table/collection named 'locations'
// Sets the 'id' column as primary.
$db ->create('locations')
    ->field('id', 'int', 'NOT NULL AUTO_INCREMENT')
    ->field('name', 'varchar(255)')
    ->field('address', 'varchar(255)')
    ->field('user_id', 'int')
    ->primaryKey('id')
    ->foreignKey('user_id', 'users', 'id')
    ->exec();

foreignKey() method

Purpose:
The foreignKey() method tells the database class that you want to create for foreign key relationship between a column on the table you are creating and a column in a different table.

Format:

foreignKey(column_name, foreign_table_name, foreign_column_name)

Examples:

// Create a table/collection named 'locations'
// Creates a relationship between the user_id column on this table and the
// 'id' column on a different 'users' table.
$db ->create('locations')
    ->field('id', 'int', 'NOT NULL AUTO_INCREMENT')
    ->field('name', 'varchar(255)')
    ->field('address', 'varchar(255)')
    ->field('user_id', 'int')
    ->primaryKey('id')
    ->foreignKey('user_id', 'users', 'id')
    ->exec();