Table operations (create, update, drop table and table schema)

Porte can manage the database through the "PorteTable" objects. Available operations include table creation and drop. Moreover, the table structure can be automatically updated to reflect the defined record classes.

Accessing a "table" object

Table objects are available from connections and records objects.

Method "create"

Create a new table, does not check if table exists unless the "drop" option is provided.

Available option includes:

Using the "create" method:


$user = new User();
// Create a new table, but an error will be thrown if table already exists
$user->table->create();
// Create a new table, drop the existing table if it exists
$user->table->create(array('drop'));
    

Method "exists"

Check if table exists.

Using the "exists" method:


$user = new User();
if($user->table->exists()){
// table exists
}else{
// table does not exist
}
    

Method "drop"

Delete a table if it exists.

Using the "exists" method:


$user = new User();
$user->table->drop();
    

The "update" method

Create the table schema for the model definitions. Handle all sort of relationships, if an existing table exists, new fields will be created but does not yet handle the migration of existing fields.

Note, field present in the database table and not referenced in the object will removed (it is planned to have this configurable).

Available options includes:

Using the "update" method:


$user = new User();
$user->table->update();
    

The "fields" method

List all the fields present in the table. The return value is an array with the field names as keys.

Tip: To get an array of all the fields:


$user = new User();
$fields = array_keys( $user->table->fields() );
    

Open Source Object Relational Mapping in PHP

Download Porte 0.2.1