Zeta Components Manual :: Docs For Class ezcDbSchema
DatabaseSchema::ezcDbSchema
Class ezcDbSchema
ezcDbSchema is the main class for schema operations.
ezcDbSchema represents the schema itself and provide proxy methods to the handlers that are able to load/save schemas from/to files, databases or other sources/destinations, depending on available schema handlers.
A database schema is a definition of all the tables inside a database, including field definitions and indexes.
The available builtin handlers are currently for MySQL, XML files and PHP arrays.
The following example shows you how you can load a database schema from the PHP format and store it into the XML format.
The following example shows how you can load a database schema from the XML format and store it into a database.
Example that shows how to make a comparison between a file on disk and a database, and how to apply the changes.
- $diff->applyToDb( $db );
Source for this file: /DatabaseSchema/src/schema.php
Version: | //autogentag// |
Constants
DATABASE
= 2
|
Used by reader and writer classes to inform that it implements a database based handler. |
FILE
= 1
|
Used by reader and writer classes to inform that it implements a file based handler. |
Member Variables
public static ezcDbSchemaOptions |
$options
Contains the options that are used by creating new schemas. |
public static array(string) |
$supportedTypes
A list of all the supported database filed types |
Method Summary
public static void |
createFromDb(
$db
)
Factory method to create a ezcDbSchema object from the database $db. |
public static void |
createFromFile(
$format
, $file
)
Factory method to create a ezcDbSchema object from the file $file with the format $format. |
public static ezcDbSchemaField |
createNewField(
$fieldType
, $fieldLength
, $fieldNotNull
, $fieldDefault
, $fieldAutoIncrement
, $fieldUnsigned
)
Returns an object to represent a table's field in the schema. |
public static ezcDbSchemaIndex |
createNewIndex(
$fields
, $primary
, $unique
)
Returns an object to represent a table's field in the schema. |
public static ezcDbSchemaIndexField |
createNewIndexField(
[ $sorting
= null] )
Returns an object to represent a table's field in the schema. |
public static ezcDbSchemaTable |
createNewTable(
$fields
, $indexes
)
Returns an object to represent a table in the schema. |
public static void |
setOptions(
$options
)
Associates an option object with this static class. |
public array(string) |
convertToDDL(
$db
)
Returns the $db specific SQL queries that would create the tables defined in the schema. |
public ezcDbSchema |
__construct(
$schema
, [ $data
= array()] )
Constructs a new ezcDbSchema object with schema definition $schema. |
public array |
getData(
)
Returns the internal data. |
public array(string=>ezcDbSchemaTable) |
&getSchema(
)
Returns the internal schema by reference. |
public void |
writeToDb(
$db
)
Creates the tables defined in the schema into the database specified through $db. |
public void |
writeToFile(
$format
, $file
)
Writes the schema to the file $file in format $format. |
Methods
createFromDb
Factory method to create a ezcDbSchema object from the database $db.
Parameters:
Name | Type | Description |
---|---|---|
$db |
ezcDbHandler |
Exceptions:
Type | Description |
---|---|
ezcDbSchemaInvalidReaderClassException |
if the handler associated with the $format is not a database schema reader. |
createFromFile
Factory method to create a ezcDbSchema object from the file $file with the format $format.
Parameters:
Name | Type | Description |
---|---|---|
$format |
string | |
$file |
string |
Exceptions:
Type | Description |
---|---|
ezcDbSchemaInvalidReaderClassException |
if the handler associated with the $format is not a file schema reader. |
createNewField
Returns an object to represent a table's field in the schema.
Parameters:
Name | Type | Description |
---|---|---|
$fieldType |
string | |
$fieldLength |
integer | |
$fieldNotNull |
bool | |
$fieldDefault |
mixed | |
$fieldAutoIncrement |
bool | |
$fieldUnsigned |
bool |
createNewIndex
Returns an object to represent a table's field in the schema.
Parameters:
Name | Type | Description |
---|---|---|
$fields |
array(string=>ezcDbSchemaIndexField) | |
$primary |
bool | |
$unique |
bool |
createNewIndexField
Returns an object to represent a table's field in the schema.
Parameters:
Name | Type | Description |
---|---|---|
$sorting |
int |
createNewTable
Returns an object to represent a table in the schema.
Parameters:
Name | Type | Description |
---|---|---|
$fields |
array(string=>ezcDbSchemaField) | |
$indexes |
array(string=>ezcDbSchemaIndex) |
setOptions
Associates an option object with this static class.
Parameters:
Name | Type | Description |
---|---|---|
$options |
ezcDbSchemaOptions |
convertToDDL
Returns the $db specific SQL queries that would create the tables defined in the schema.
The database type can be given as both a database handler (instanceof ezcDbHandler) or the name of the database as string as retrieved through calling getName() on the database handler object.
Parameters:
Name | Type | Description |
---|---|---|
$db |
string|ezcDbHandler |
Exceptions:
Type | Description |
---|---|
ezcDbSchemaInvalidWriterClassException |
if the handler associated with the $format is not a database schema writer. |
__construct
Constructs a new ezcDbSchema object with schema definition $schema.
Parameters:
Name | Type | Description |
---|---|---|
$schema |
array(ezcDbSchemaTable) | |
$data |
array |
getData
Returns the internal data.
This data is not used anywhere though.
getSchema
Returns the internal schema by reference.
The method returns an array where the key is the table name, and the value the table definition stored in a ezcDbSchemaTable struct.
writeToDb
Creates the tables defined in the schema into the database specified through $db.
Parameters:
Name | Type | Description |
---|---|---|
$db |
ezcDbHandler |
Exceptions:
Type | Description |
---|---|
ezcDbSchemaInvalidWriterClassException |
if the handler associated with the $format is not a database schema writer. |
writeToFile
Writes the schema to the file $file in format $format.
Parameters:
Name | Type | Description |
---|---|---|
$format |
string | Available formats are at least: 'array' and 'xml'. |
$file |
string |
Exceptions:
Type | Description |
---|---|
ezcDbSchemaInvalidWriterClassException |
if the handler associated with the $format is not a file schema writer. |