View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFPROP:Proposal Zone Template}

{zone-data:component-name}
Zend_Db_Schema
{zone-data}

{zone-data:proposer-list}
[Maksym Sliesarenko|mailto:maks.slesarenko@gmail.com]
{zone-data}

{zone-data:liaison}
TBD
{zone-data}

{zone-data:revision}
1.0 - 13 May 2012: Initial Draft.
{zone-data}

{zone-data:overview}
Zend_Db_Schema is a simple component that allows you to manage your db entity's schema
{zone-data}

{zone-data:references}
* [Generated Doc | http://maksslesarenko.github.com/ZendDbSchema/]
* [Source | https://github.com/MaksSlesarenko/ZendDbSchema]
{zone-data}

{zone-data:requirements}

* This component *will* provide ability to alter database entities.
* This component *will* provide ability to represent database entities as php objects.

{zone-data}

{zone-data:dependencies}
* Zend_Db
* Zend_Validate
* Zend_Filter
* Zend_Tool
{zone-data}

{zone-data:operation}
The component is instantiated with a mind-link that ...
{zone-data}

{zone-data:milestones}
Describe some intermediate state of this component in terms of design notes, additional material added to this page, and / code. Note any significant dependencies here, such as, "Milestone #3 can not be completed until feature Foo has been added to ZF component XYZ." Milestones will be required for acceptance of future proposals. They are not hard, and many times you will only need to think of the first three below.
* Milestone 1: [design notes will be published here|http://framework.zend.com/wiki/x/sg]
* Milestone 2: Working prototype checked into the incubator supporting use cases #1, #2, ...
* Milestone 3: Working prototype checked into the incubator supporting use cases #3 and #4.
* Milestone 4: Unit tests exist, work, and are checked into SVN.
* Milestone 5: Initial documentation exists.

If a milestone is already done, begin the description with "\[DONE\]", like this:
* Milestone #: \[DONE\] Unit tests ...
{zone-data}

{zone-data:class-list}
Zend_Db_Migration_Change
Zend_Db_Migration_Exception
Zend_Db_Migration_Manager
Zend_Db_Migration_Skeleton
Zend_Db_Schema_AbstractEntity
Zend_Db_Schema_AbstractSchema
Zend_Db_Schema_Database
Zend_Db_Schema_Exception
Zend_Db_Schema_Generator_Interface
Zend_Db_Schema_Generator_Mysql
Zend_Db_Schema_Loader_Interface
Zend_Db_Schema_Loader_Mysql
Zend_Db_Schema_Table
Zend_Db_Schema_Table_AbstractDefinition
Zend_Db_Schema_Table_Column
Zend_Db_Schema_Table_DefinitionManager_AbstractManager
Zend_Db_Schema_Table_DefinitionManager_Column
Zend_Db_Schema_Table_DefinitionManager_ForeignKey
Zend_Db_Schema_Table_DefinitionManager_Index
Zend_Db_Schema_Table_ForeignKey
Zend_Db_Schema_Table_Index
Zend_Db_Schema_Table_PrimaryKey
Zend_Tool_Project_Context_Migration_Directory
Zend_Tool_Project_Context_Migration_File
Zend_Tool_Project_Context_Schema_Directory
Zend_Tool_Project_Context_Schema_File
Zend_Tool_Project_Provider_Migration
Zend_Tool_Project_Provider_Schema
Zend_Validate_Db_Schema_AbstractDatabase
Zend_Validate_Db_Schema_AbstractTable
Zend_Validate_Db_Schema_Chain
Zend_Validate_Db_Schema_Database_Mysql_Charset
Zend_Validate_Db_Schema_Database_Mysql_Name
Zend_Validate_Db_Schema_Database_MysqlDatabase
Zend_Validate_Db_Schema_Table_Mysql_Column
Zend_Validate_Db_Schema_Table_Mysql_Column_Name
Zend_Validate_Db_Schema_Table_Mysql_Column_Type
Zend_Validate_Db_Schema_Table_Mysql_Engine
Zend_Validate_Db_Schema_Table_Mysql_Index_Name
Zend_Validate_Db_Schema_Table_Mysql_Index_Type
Zend_Validate_Db_Schema_Table_Mysql_Name
Zend_Validate_Db_Schema_Table_MysqlTable
{zone-data}

{zone-data:use-cases}
||UC-01||

Zend_Loader_Autoloader::getInstance()->registerNamespace('ZendDbSchema');

$table= new ZendDbSchema_Db_Schema_Table('users');

$table->engine = 'myisam'; //change param
$table->save();

$table->toArray(); //export to array
$table->toSql(); //export to sql (CREATE statement)
$table->toSql(true); //export to sql (ALTER statement)

$database= new ZendDbSchema_Db_Schema_Database('test');
$database->__toString();// alias for toSql method

{zone-data}

{zone-data:skeletons}

{composition-setup}

{deck:id=Skeletons}

{card:label=AbstractSchema}
{code:type=php}
<?php
abstract class ZendDbSchema_Db_Schema_AbstractSchema
implements IteratorAggregate
{
const NAME_KEY = 'name';

/**
* Schema params
*
* @var array
*/
protected $_params = array();

/**
* @var string
*/
protected $_name;

/**
* Get clean schema
*
* @return array
*/
abstract protected function _getCleanSchema();

/**
* Get sql query
*
* @return string
*/
abstract protected function _getAlterSql();

/**
* Get sql query
*
* @return string
*/
abstract protected function _getCreateSql();

/**
* Get sql query
*
* @return string
*/
abstract protected function _getDropSql();

/**
* Check if schema is valid
*
* @return boolen
*/
abstract function isValid();

/**
* Check if entity exists in database
*
* @return boolen
*/
public function isExist();

/**
* Is dirty schema or param
*
* @param string|null $param
* @return boolean
*/
public function isDirty($param = null);

/**
* Get clean schema
*
* @param string $param
* @return array|null
*/
public function getCleanSchema($param = null);

/**
* Refresh database schema
*
* @param string $param
* @return self
*/
public function refresh($param = null);

/**
* Constructor
*
* @param string $name
* @throws ZendDbSchema_Db_Schema_Exception
*/
public function __construct($name = null);

/**
* Initialize object
*
* Called from {@link __construct()} as final step of object instantiation.
*
* @return void
*/
public function init();

/**
* Get sql representation of schema
*
* @return string
*/
public function __toString();

/**
* Retrieve database param value
*
* @param string $paramName
* @return string
* @throws ZendDbSchema_Db_Schema_Exception if the $name is not a param in the schema.
*/
public function __get($name);

/**
* Test existence of row field
*
* @param string $name
* @return boolean
*/
public function __isset($name);

/**
* Set schema params
*
* @param string $name
* @param array $value
*/
public function __set($name, $value);

/**
* Unset row field value
*
* @param string $name
* @return self
* @throws ZendDbSchema_Db_Schema_Exception
*/
public function __unset($name);

/**
* Clone schema
*
*/
public function __clone();

/**
* Get iterator
*
* Required by the IteratorAggregate implementation
*
* @return ArrayIterator
*/
public function getIterator();

/**
* Set schema from array
*
* @param array $schema
* @return self
*/
public function setFromArray(array $schema);

/**
* To array
*
* @return array
*/
public function toArray();

/**
* Set name
*
* @param string $name
* @return self
*/
public function setName($name);

/**
* Get name
*
* @return string
*/
public function getName();
/**
* Get name
*
* @return string
*/
public function getOriginName();

/**
* Get sql query
*
* @return string
*/
public function getDropSql();

/**
* Get sql representation
*
* @param boolen $alter
* @return string
*/
public function toSql($alter = false);
}
?>
{code}
{card}


{card:label=AbstractEntity}
{code:type=php}
<?php
abstract class ZendDbSchema_Db_Schema_AbstractEntity
extends ZendDbSchema_Db_Schema_AbstractSchema
{
/**
* @var Zend_Db_Adapter_Abstract
*/
protected $_adapter;

/**
* @var ZendDbSchema_Db_Schema_Loader_Interface
*/
protected $_loader;

/**
* @var Zend_Validate_Abstract
*/
protected $_validator;

/**
* @var ZendDbSchema_Db_Schema_Generator_Interface
*/
protected $_generator;

/**
* Full schema loaded from db
*
* @var array
*/
protected $_cleanSchema = array();

/**
* Set schema validator
*
* @param Zend_Validate_Abstract $validator
* @return self
*/
abstract function setValidator(Zend_Validate_Interface $validator);

/**
* Reload schema
*
* @return boolen
*/
abstract protected function _doReload();

/**
* Allows pre-delete logic to be applied to database.
* Subclasses may override this method.
*
* @return void
*/
protected function _delete();

/**
* Allows post-insert logic to be applied to database.
* Subclasses may override this method.
*
* @return void
*/
protected function _postDelete();

/**
* Allows pre-save logic to be applied to database.
* Subclasses may override this method.
*
* @return void
*/
protected function _save();

/**
* Allows post-save logic to be applied to database.
* Subclasses may override this method.
*
* @return void
*/
protected function _postSave();

/**
* Refresh schema
*
* @return self
*/
public function reload();

/**
* Refresh database schema
*
* @param string $param
* @return self
*/
public function refresh($param = null);

/**
* Get clean schema
*
* @return array
*/
protected function _getCleanSchema();

/**
* Constructor
*
* @param string $name
* @param ZendDbSchema_Db_Adapter_SchemaCompatible $adapter
* @param ZendDbSchema_Db_Schema_Loader_Interface $loader
* @param ZendDbSchema_Db_Schema_Generator_Interface $generator
* @throws ZendDbSchema_Db_Schema_Exception
*/
public function __construct(
$name = null,
ZendDbSchema_Db_Adapter_SchemaCompatible $adapter = null,
ZendDbSchema_Db_Schema_Loader_Interface $loader = null,
ZendDbSchema_Db_Schema_Generator_Interface $generator = null);

/**
* Setup to do on wakeup.
* A de-serialized Row should not be assumed to have access to a live
* database connection, so set _connected = false.
*
* @return void
*/
public function __wakeup();

/**
* Exec sql code
*
* @param string $sql
* @return self
*/
public function exec($sql);

/**
* Save schema to db
*
* @return boolen
*/
public function save();

/**
* Delete schema from db
*
* @return self
*/
public function delete();

/**
* Validate schema
*
* @return boolen
*/
public function isValid();

/**
* Get validator
*
* @return Zend_Validate_Abstract
*/
public function getValidator();

/**
* Get sql generator
*
* @return ZendDbSchema_Db_Schema_Generator_Interface
*/
public function getGenerator();

/**
* Get schema config loader
*
* @return ZendDbSchema_Db_Schema_Loader_Interface
*/
public function getLoader();

/**
* Get db adapter
*
* @return Zend_Db_Adapter_Abstract
*/
public function getAdapter();
}
?>
{code}
{card}


{card:label=Database}
{code:type=php}
<?php
class ZendDbSchema_Db_Schema_Database
extends ZendDbSchema_Db_Schema_AbstractEntity
{
const CHARSET_KEY = 'charset';

/**
* Set schema validator
*
* @param ZendDbSchema_Validate_Db_Schema_AbstractDatabase $validator
* @return self
*/
public function setValidator(Zend_Validate_Interface $validator);

/**
* @see ZendDbSchema_Db_Schema_AbstractEntity::_getAlterSql()
*/
protected function _getAlterSql();

/**
* @see ZendDbSchema_Db_Schema_AbstractEntity::_getCreateSql()
*/
protected function _getCreateSql();

/**
* @see ZendDbSchema_Db_Schema_AbstractSchema::_getDropSql()
*/
protected function _getDropSql();

/**
* Reload database schema
*
* @return boolen
*/
protected function _doReload();

/**
* Set charset
*
* @param string $name
* @return self
*/
public function setCharset($charset);

/**
* Get charset
*
* @return string
*/
public function getCharset();
}
?>
{code}
{card}

{card:label=Table}
{code:type=php}
<?php
class ZendDbSchema_Db_Schema_Table extends ZendDbSchema_Db_Schema_AbstractEntity
{
const CHARSET_KEY = 'charset';

const COLUMNS_KEY = 'columns';
const INDEXES_KEY = 'indexes';
const FOREIGNS_KEY = 'foreigns';
const PRIMARY_KEY = 'primary';

/**
* @var string
*/
protected $_columnsManagerClass = 'ZendDbSchema_Db_Schema_Table_DefinitionManager_Column';

/**
* @var string
*/
protected $_indexesManagerClass = 'ZendDbSchema_Db_Schema_Table_DefinitionManager_Index';

/**
* @var string
*/
protected $_foreignsManagerClass = 'ZendDbSchema_Db_Schema_Table_DefinitionManager_ForeignKey';

/**
* @var string
*/
protected $_columnClass = 'ZendDbSchema_Db_Schema_Table_Column';

/**
* @var string
*/
protected $_indexClass = 'ZendDbSchema_Db_Schema_Table_Index';

/**
* @var string
*/
protected $_foreignClass = 'ZendDbSchema_Db_Schema_Table_ForeignKey';

/**
* @var string
*/
protected $_primaryKeyClass = 'ZendDbSchema_Db_Schema_Table_PrimaryKey';

/**
* Table columns schema
*
* @var ZendDbSchema_Db_Schema_Table_DefinitionManager_Column
*/
protected $_columns;

/**
* Table columns schema
*
* @var ZendDbSchema_Db_Schema_Table_DefinitionManager_Index
*/
protected $_indexes;

/**
* Table columns schema
*
* @var ZendDbSchema_Db_Schema_Table_DefinitionManager_Index
*/
protected $_foreigns;

/**
* Primary keys
*
* @var ZendDbSchema_Db_Schema_Table_PrimaryKey
*/
protected $_primary;

/**
* Set schema validator
*
* @param ZendDbSchema_Validate_Db_Schema_AbstractTable $validator
* @return self
*/
public function setValidator(Zend_Validate_Interface $validator);

/**
* Get column classname
*
* @return string
*/
public function getColumnClass();

/**
* Get index classname
*
* @return string
*/
public function getIndexClass();

/**
* Get foreign key classname
*
* @return string
*/
public function getForeignClass();

/**
* Set schema from array
*
* @param array $schema
* @return self
*/
public function setFromArray(array $schema);

/**
* To array
*
* @return array
*/
public function toArray();

/**
* Get columns
*
* @return ZendDbSchema_Db_Schema_Table_DefinitionManager_Column
*/
public function columns();

/**
* Get or replace primary key
*
* @param ZendDbSchema_Db_Schema_Table_PrimaryKey $primary
* @return ZendDbSchema_Db_Schema_Table_PrimaryKey
*/
public function primary(ZendDbSchema_Db_Schema_Table_PrimaryKey $primary = null);

/**
* Get indexes
*
* @return ZendDbSchema_Db_Schema_Table_DefinitionManager_Index
*/
public function indexes();

/**
* Get forign keys
*
* @return ZendDbSchema_Db_Schema_Table_DefinitionManager_Foreign
*/
public function foreigns();

/**
* Get sql query
*
* @param boolen $alter
* @return string
*/
public function toSql($alter = false);

/**
* Is dirty schema or param
*
* @param string|null $param
* @return boolean
*/
public function isDirty($param = null);

/**
* Add table definition
*
* @param ZendDbSchema_Db_Schema_Table_AbstractDefinition $item
* @return ZendDbSchema_Db_Schema_Table
*/
public function add(ZendDbSchema_Db_Schema_Table_AbstractDefinition $item);

/**
* @see ZendDbSchema_Db_Schema_AbstractEntity::_getAlterSql()
*/
protected function _getAlterSql();

/**
* @see ZendDbSchema_Db_Schema_AbstractEntity::_getCreateSql()
*/
protected function _getCreateSql();

/**
* @see ZendDbSchema_Db_Schema_AbstractSchema::_getDropSql()
*/
protected function _getDropSql();

/**
* Refresh table schema
*
* @return boolen
*/
protected function _doReload();
}
?>
{code}
{card}
{deck}

{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>