Thursday, August 7. 2008eZ Components Add-on: Manipulate Database Schemas
As written in my blogpost about my Content Storage Component Project, I needed an easy way to dynamically manipulate database schemas from inside a PHP application.
Therefor I started and finished now an add-on to the Database Schema eZComponent. I discussed the possibility of inclusion this add-on in the official eZComponent, but it was rejected. Derick does not see a need for database manipulation in the way I intend and I agree that it is often better not to include a feature to keep Software slim and maintainable. You can get the source from my Git repository. Below is a first draft of the tutorial. More information on my content storage idea can be grapped from a Presentation Draft. ymc eZ Components Add-on - DatabaseSchemaIntroductionThis addon is meant to ease the manipulation of database schemes by PHP applications. While the plain DatabaseSchema eZ Component is meant to read, compare and apply database schemes, it is not easy to use it for simple manipulations, e.g. adding a column, droping a table, adding a table. The question whether an application should modify its database schema in this way is still a subject of discussion, but not the subject of this tutorial. UsageThere is only one intended workflow for this component:
The schema definition methods are modeled after SQL. See an example: CODE: <?php
require_once 'ezc/Base/ezc_bootstrap.php';
$repDir = dirname( _FILE_ ).'/../src';
ezcBase::addClassRepository( $repDir, $repDir );
$api = new ymcDbSchema;
// build the schema
$api->createTable( 'product' )
->column( 'id' )
->autoIncrement()
->notNull()
->column( 'description', ymcDbSchema::TEXT )
->length( 1024 )
->column( 'prize' )
->createTable( 'stock' )
->column( 'id' )
->autoIncrement()
->notNull()
->column( 'product_id' )
->notNull()
->column( 'quantity' )
->notNull()
;
// get an ezcDbSchema object
$schema = $api->getSchema(); The resulting ezcDbSchema object can now be used as described in the documentation of eZ Components DatabaseSchema. More often you may use this add-on to modify an existing schema. So you need an ezcDbSchemaDiff object instead: CODE: <?php
require_once 'ezc/Base/ezc_bootstrap.php';
$repDir = dirname( _FILE_ ).'/../src';
ezcBase::addClassRepository( $repDir, $repDir );
$api = new ymcDbSchema;
// build the schema
$api->dropTable( 'stock' )
->alterTable( 'product' )
->addColumn( 'supplier_id' )
->notNull()
;
// get an ezcDbSchemaDiff object
$schemaDiff = $api->getSchemaDiff(); Please refer again to the documentation of eZ Components DatabaseSchema for details about the ezcDbSchemaDiff object. Trackbacks
No Trackbacks
|
Links |