The Manager module provides methods for managing database structure. The methods can be grouped based on their responsibility: create, edit (alter or update), list or delete (drop) database elements. The following document lists the available methods, providing examples of their use. The following tables will be created, altered and finally dropped, in a database named "events_db":
events(id, name, datetime); people(id, name); event_participants(event_id, person_id); |
Example 39-1. Loading the Manager module
|
Example 39-2. Creating a database
|
These are methods to create new databases, tables, indices, constraints and sequences.
Example 39-3. Creating and setting an events_db database
|
Now that the database is created, we can proceed with adding some tables. The method createTable() takes three parameters: the table name, an array of field definition and some extra options (optional and RDBMS-specific).
Example 39-4. Creating the events table
|
Table 39-1. Options for the definition array based on datatype
Datatype | length | default | not null | unsigned |
---|---|---|---|---|
text | x | x | x | |
boolean | x | x | ||
integer | x | x | x | |
decimal | x | x | ||
float | x | x | ||
timestamp | x | x | ||
time | x | x | ||
date | x | x | ||
clob | x | x | ||
blob | x | x |
Example 39-5. Creating the people table
|
Example 39-6. Creating the event_participants table
|
In the example events table, the id should be a primary key. Creating a primary key is a task done by the createConstraint() method. It takes three parameters: the table name, the constraint name and the definition array.
The full structure of the definition array looks like this (in this case, it's representing a FOREIGN KEY constraint):
<?php $definition = array ( 'primary' => false, 'unique' => false, 'foreign' => true, 'check' => false, 'fields' => array ( 'field1name' => array(), // one entry per each field covered 'field2name' => array(), // by the index 'field3name' => array( 'sorting' => ascending|descending, 'position' => 3, ), ) 'references' => array( 'table' => name, 'fields' => array( 'field1name' => array( //one entry per each referenced field 'position' => 1, ), ) 'deferrable' => false, 'initiallydeferred' => false, 'onupdate' => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION, 'ondelete' => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION, 'match' => SIMPLE|PARTIAL|FULL, ); ?> |
Example 39-7. Creating primary keys in the events and people tables
|
In the definition array, you specify which fields will be included in the constraint, using the fields key. The other possible keys in the definition array are primary and unique, which have boolean values.
Let's create another key in the event_participants, where each row has to be unique, meaning that one person can appear only once for a specific event. The definitions array will have both fields in the fields key and the unique key will be set to true.
Example 39-8. Creating a unique constraint
|
To create an index, the method createIndex() is used, which has similar signature as createConstraint(), so it takes table name, index name and a definition array. The definition array has one key fields with a value which is another associative array containing fields that will be a part of the index. The fields are defined as arrays with possible keys:
sorting, with values ascending and descending
length, integer value
Example 39-9. Creating an index
|
The way MDB2 handles sequences is described here. For the events table in our example database, we'll need to have the 'id' auto-incrementing. For this purpose the method nextId() is used to give the next value. nextId() will create the sequence table if it doesn't exist, but we can create if beforehand with createSequence(). It takes a sequence name and an optional start value for the sequence.
Example 39-10. Creating sequences
|
Once a database table is created you can rename it or add, remove, rename and alter fields, using the alterTable() method. alterTable() takes three parameters: the table name, the definition of changes and a boolean "check-only" flag. If true, no changes will be made, but only a check if the proposed changes are feasible for the specific table and RDBMS. The second parameter (definition of changes) is an array with these keys:
name: new name for the table. This is the only key related to the table itself, the other keys contain field definitions
add: fields to be added
remove: fields to be removed
change: fields to be changed
rename: fields to be renamed
To see what's in the database, you can use the list*() family of functions, namely:
listDatabases()
listFunctions()
listSequences(): takes optional database name as a parameter. If not supplied, the currently selected database is assumed.
listTableConstraints(): takes a table name
listTableFields(): takes a table name
listTableIndexes(): takes a table name
listTables(): takes an optional database name
listTableTriggers(): takes a table name
listTableViews(): takes a table name
listUsers()
listViews(): takes an optional database name
Example 39-11. Listing database elements
|
For every create*() method as shown above, there is a corresponding drop*() method to delete a database, a table, field, index or constraint. The drop*() methods do not check if the item to be deleted exists, so it's the developer's responsibility to check for PEAR errors.
Example 39-12. Drop database elements
|