2008-01-05 22:55:56 +03:00
|
|
|
<?php
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* A table object holds all the information (meta data) of a database table and it's relations
|
|
|
|
* These informations are needed for the proper object-relational mapping of the domain classes.
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
2008-01-05 22:55:56 +03:00
|
|
|
* @package Doctrine
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
class Doctrine_Table extends Doctrine_Configurable implements Serializable
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* The name of the domain class that is mapped to the database table with this metadata.
|
|
|
|
* Note: In Single Table Inheritance this will be the name of the root class of the
|
|
|
|
* hierarchy (the one that gets the database table).
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
protected $_domainClassName;
|
|
|
|
|
|
|
|
protected $_conn;
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2007-11-18 19:06:37 +03:00
|
|
|
* @var mixed $identifier The field names of all fields that are part of the identifier/primary key
|
2008-01-05 22:55:56 +03:00
|
|
|
* of the described component.
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
protected $_identifier = array();
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* The identifier type of the component.
|
|
|
|
*
|
2007-05-28 00:00:53 +04:00
|
|
|
* @see Doctrine_Identifier constants
|
2008-01-05 22:55:56 +03:00
|
|
|
* @var integer $identifierType
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2007-10-16 01:39:35 +04:00
|
|
|
protected $_identifierType;
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
*
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
protected $_inheritanceType = Doctrine::INHERITANCETYPE_TABLE_PER_CLASS;
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* @see Doctrine_Template
|
|
|
|
* @var array $_templates an array containing all templates attached to this table
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
protected $_templates = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* @see Doctrine_Record_Filter
|
|
|
|
* @var array $_filters an array containing all record filters attached to this table
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
protected $_filters = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Doctrine_Record_Generator
|
|
|
|
* @var array $_generators an array containing all generators attached to this table
|
|
|
|
*/
|
|
|
|
protected $_generators = array();
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var array $columns an array of column definitions,
|
2007-11-18 19:06:37 +03:00
|
|
|
* keys are column names and values are column definitions
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
2007-07-10 03:01:59 +04:00
|
|
|
* the definition array has atleast the following values:
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
2007-07-10 03:01:59 +04:00
|
|
|
* -- type the column type, eg. 'integer'
|
|
|
|
* -- length the column length, eg. 11
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
2007-07-10 03:01:59 +04:00
|
|
|
* additional keys:
|
|
|
|
* -- notnull whether or not the column is marked as notnull
|
|
|
|
* -- values enum values
|
|
|
|
* -- notblank notblank validator + notnull constraint
|
|
|
|
* ... many more
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
protected $_columns = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2007-11-18 19:06:37 +03:00
|
|
|
* @var array $_fieldNames an array of field names. used to look up field names
|
|
|
|
* from column names.
|
|
|
|
* keys are column names and values are field names
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
protected $_fieldNames = array();
|
2007-11-18 19:06:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var array $_columnNames an array of column names
|
|
|
|
* keys are field names and values column names.
|
|
|
|
* used to look up column names from field names.
|
|
|
|
* this is the reverse lookup map of $_fieldNames.
|
|
|
|
*/
|
|
|
|
protected $_columnNames = array();
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Doctrine_Tree $tree tree object associated with this table
|
|
|
|
*/
|
|
|
|
protected $_tree;
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var integer $columnCount cached column count, Doctrine_Record uses this column count in when
|
|
|
|
* determining its state
|
|
|
|
*/
|
2007-10-16 01:39:35 +04:00
|
|
|
protected $columnCount;
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var boolean $hasDefaultValues whether or not this table has default values
|
|
|
|
*/
|
2007-10-16 01:39:35 +04:00
|
|
|
protected $hasDefaultValues;
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Doctrine_Relation_Parser $_parser relation parser object
|
|
|
|
*/
|
|
|
|
protected $_parser;
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var array $options an array containing all options
|
|
|
|
*
|
|
|
|
* -- name name of the component, for example component name of the GroupTable is 'Group'
|
|
|
|
*
|
|
|
|
* -- parents the parent classes of this component
|
|
|
|
*
|
|
|
|
* -- declaringClass name of the table definition declaring class (when using inheritance the class
|
2007-09-02 12:53:16 +04:00
|
|
|
* that defines the table structure can be any class in the inheritance hierarchy,
|
2007-05-28 00:00:53 +04:00
|
|
|
* hence we need reflection to check out which class actually calls setTableDefinition)
|
|
|
|
*
|
|
|
|
* -- tableName database table name, in most cases this is the same as component name but in some cases
|
|
|
|
* where one-table-multi-class inheritance is used this will be the name of the inherited table
|
|
|
|
*
|
|
|
|
* -- sequenceName Some databases need sequences instead of auto incrementation primary keys,
|
|
|
|
* you can set specific sequence for your table by calling setOption('sequenceName', $seqName)
|
|
|
|
* where $seqName is the name of the desired sequence
|
|
|
|
*
|
|
|
|
* -- enumMap enum value arrays
|
|
|
|
*
|
2008-01-05 22:55:56 +03:00
|
|
|
* -- inheritanceMap contains the mapping of the discriminator column (which discriminator value identifies
|
|
|
|
* which class). Used in Single & Class Table Inheritance.
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
|
|
|
* -- type table type (mysql example: INNODB)
|
|
|
|
*
|
|
|
|
* -- charset character set
|
|
|
|
*
|
|
|
|
* -- foreignKeys the foreign keys of this table
|
|
|
|
*
|
2007-07-24 03:18:11 +04:00
|
|
|
* -- checks the check constraints of this table, eg. 'price > dicounted_price'
|
|
|
|
*
|
|
|
|
* -- collation collation attribute
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
|
|
|
* -- indexes the index definitions of this table
|
|
|
|
*
|
|
|
|
* -- treeImpl the tree implementation of this table (if any)
|
|
|
|
*
|
|
|
|
* -- treeOptions the tree options
|
2007-06-07 21:04:56 +04:00
|
|
|
*
|
2007-10-06 01:18:40 +04:00
|
|
|
* -- queryParts the bound query parts
|
|
|
|
*
|
2007-06-07 21:04:56 +04:00
|
|
|
* -- versioning
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
protected $_options = array(
|
|
|
|
'tableName' => null,
|
|
|
|
'sequenceName' => null,
|
|
|
|
'inheritanceType' => null,
|
|
|
|
'inheritanceMap' => array(),
|
|
|
|
'enumMap' => array(),
|
|
|
|
'type' => null,
|
|
|
|
'charset' => null,
|
|
|
|
'collation' => null,
|
|
|
|
'collate' => null,
|
|
|
|
'treeImpl' => null,
|
|
|
|
'treeOptions' => null,
|
|
|
|
'subclasses' => null,
|
|
|
|
'queryParts' => array(),
|
|
|
|
'indexes' => array(),
|
|
|
|
'parents' => array(),
|
|
|
|
'joinedParents' => array()
|
|
|
|
);
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* Constructs a new table object.
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
public function __construct($domainClassName, Doctrine_Connection $conn)
|
|
|
|
{
|
|
|
|
$this->_domainClassName = $domainClassName;
|
2007-09-21 00:21:08 +04:00
|
|
|
$this->_conn = $conn;
|
2007-05-28 00:00:53 +04:00
|
|
|
$this->_parser = new Doctrine_Relation_Parser($this);
|
2007-10-16 01:39:35 +04:00
|
|
|
$this->_filters[] = new Doctrine_Record_Filter_Standard();
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->setParent($this->_conn);
|
2007-10-16 00:43:24 +04:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
public function getConnection()
|
2007-10-16 00:43:24 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
return $this->_conn;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the owner of a column.
|
|
|
|
* The owner of a column is the name of the component in a hierarchy that
|
|
|
|
* defines the column.
|
|
|
|
*
|
|
|
|
* @param string $columnName The column name
|
|
|
|
* @return string The name of the owning/defining component
|
|
|
|
*/
|
|
|
|
public function getColumnOwner($columnName)
|
2007-11-10 16:21:40 +03:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
if (isset($this->_columns[$columnName]['owner'])) {
|
|
|
|
return $this->_columns[$columnName]['owner'];
|
2007-11-10 16:21:40 +03:00
|
|
|
} else {
|
|
|
|
return $this->getComponentName();
|
|
|
|
}
|
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
/**
|
|
|
|
* getComponentName
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function getComponentName()
|
|
|
|
{
|
|
|
|
//return $this->_options['name'];
|
|
|
|
return $this->_domainClassName;
|
|
|
|
}
|
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
/**
|
|
|
|
* Checks whether a column is inherited from a component further up in the hierarchy.
|
|
|
|
*
|
|
|
|
* @param $columnName The column name
|
|
|
|
* @return boolean TRUE if column is inherited, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
public function isInheritedColumn($columnName)
|
2007-11-10 16:21:40 +03:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
return (isset($this->_columns[$columnName]['owner']));
|
2007-11-10 16:21:40 +03:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a field is part of the table identifier/primary key field(s).
|
|
|
|
*
|
|
|
|
* @param string $fieldName The field name
|
|
|
|
* @return boolean TRUE if the field is part of the table identifier/primary key field(s),
|
|
|
|
* FALSE otherwise.
|
|
|
|
*/
|
|
|
|
public function isIdentifier($fieldName)
|
2007-10-22 21:33:47 +04:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
return ($fieldName === $this->getIdentifier() ||
|
|
|
|
in_array($fieldName, (array) $this->getIdentifier()));
|
2007-10-22 21:33:47 +04:00
|
|
|
}
|
2007-10-08 03:36:28 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
/**
|
|
|
|
* addForeignKey
|
|
|
|
*
|
|
|
|
* adds a foreignKey to this table
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addForeignKey(array $definition)
|
2007-10-08 03:36:28 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->_options['foreignKeys'][] = $definition;
|
2007-10-08 03:36:28 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-08-27 19:04:32 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* addCheckConstraint
|
2007-08-27 19:04:32 +04:00
|
|
|
*
|
2008-01-05 22:55:56 +03:00
|
|
|
* adds a check constraint to this table
|
|
|
|
*
|
|
|
|
* @return void
|
2007-08-27 19:04:32 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
public function addCheckConstraint($definition, $name)
|
2007-08-27 19:04:32 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
if (is_string($name)) {
|
|
|
|
$this->_options['checks'][$name] = $definition;
|
|
|
|
} else {
|
|
|
|
$this->_options['checks'][] = $definition;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* addIndex
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
2008-01-05 22:55:56 +03:00
|
|
|
* adds an index to this table
|
|
|
|
*
|
|
|
|
* @return void
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
public function addIndex($index, array $definition)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->_options['indexes'][$index] = $definition;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-06 01:00:39 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* getIndex
|
2007-06-06 01:00:39 +04:00
|
|
|
*
|
2008-01-05 22:55:56 +03:00
|
|
|
* @return array|boolean array on success, FALSE on failure
|
2007-06-06 01:00:39 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
public function getIndex($index)
|
2007-06-06 01:00:39 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
if (isset($this->_options['indexes'][$index])) {
|
|
|
|
return $this->_options['indexes'][$index];
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* setOption
|
2007-09-02 12:53:16 +04:00
|
|
|
* sets an option and returns this object in order to
|
2007-05-28 00:00:53 +04:00
|
|
|
* allow flexible method chaining
|
|
|
|
*
|
|
|
|
* @see Doctrine_Table::$_options for available options
|
|
|
|
* @param string $name the name of the option to set
|
|
|
|
* @param mixed $value the value of the option
|
|
|
|
* @return Doctrine_Table this object
|
|
|
|
*/
|
|
|
|
public function setOption($name, $value)
|
|
|
|
{
|
|
|
|
switch ($name) {
|
|
|
|
case 'name':
|
|
|
|
case 'tableName':
|
|
|
|
break;
|
|
|
|
case 'enumMap':
|
|
|
|
case 'inheritanceMap':
|
|
|
|
case 'index':
|
|
|
|
case 'treeOptions':
|
|
|
|
if ( ! is_array($value)) {
|
|
|
|
throw new Doctrine_Table_Exception($name . ' should be an array.');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-10-06 01:18:40 +04:00
|
|
|
$this->_options[$name] = $value;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getOption
|
|
|
|
* returns the value of given option
|
|
|
|
*
|
|
|
|
* @param string $name the name of the option
|
|
|
|
* @return mixed the value of given option
|
|
|
|
*/
|
|
|
|
public function getOption($name)
|
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
if (isset($this->_options[$name])) {
|
|
|
|
return $this->_options[$name];
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getOptions
|
|
|
|
* returns all options of this table and the associated values
|
|
|
|
*
|
|
|
|
* @return array all options and their values
|
|
|
|
*/
|
|
|
|
public function getOptions()
|
|
|
|
{
|
|
|
|
return $this->_options;
|
|
|
|
}
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getColumnName
|
|
|
|
*
|
|
|
|
* returns a column name for column alias
|
|
|
|
* if the actual name for the alias cannot be found
|
|
|
|
* this method returns the given alias
|
|
|
|
*
|
|
|
|
* @param string $alias column alias
|
|
|
|
* @return string column name
|
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
public function getColumnName($fieldName)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
if (isset($this->_columnNames[$fieldName])) {
|
|
|
|
return $this->_columnNames[$fieldName];
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
return $fieldName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getColumnDefinition($columnName)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_columns[$columnName])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->_columns[$columnName];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getColumnAlias
|
|
|
|
*
|
|
|
|
* returns a column alias for a column name
|
|
|
|
* if no alias can be found the column name is returned.
|
|
|
|
*
|
|
|
|
* @param string $columnName column name
|
|
|
|
* @return string column alias
|
|
|
|
*/
|
|
|
|
public function getFieldName($columnName)
|
|
|
|
{
|
|
|
|
if (isset($this->_fieldNames[$columnName])) {
|
|
|
|
return $this->_fieldNames[$columnName];
|
|
|
|
}
|
|
|
|
return $columnName;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-11-27 19:08:34 +03:00
|
|
|
public function setColumns(array $definitions)
|
|
|
|
{
|
|
|
|
foreach ($definitions as $name => $options) {
|
|
|
|
$this->setColumn($name, $options['type'], $options['length'], $options);
|
|
|
|
}
|
|
|
|
}
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* setColumn
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $type
|
|
|
|
* @param integer $length
|
|
|
|
* @param mixed $options
|
2007-11-18 19:06:37 +03:00
|
|
|
* @param boolean $prepend Whether to prepend or append the new column to the column list.
|
|
|
|
* By default the column gets appended.
|
2007-05-28 00:00:53 +04:00
|
|
|
* @throws Doctrine_Table_Exception if trying use wrongly typed parameter
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
public function setColumn($name, $type, $length = null, $options = array(), $prepend = false)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
|
|
|
if (is_string($options)) {
|
|
|
|
$options = explode('|', $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($options as $k => $option) {
|
|
|
|
if (is_numeric($k)) {
|
|
|
|
if ( ! empty($option)) {
|
|
|
|
$options[$option] = true;
|
|
|
|
}
|
|
|
|
unset($options[$k]);
|
|
|
|
}
|
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
|
|
|
|
// extract column name & field name
|
2007-05-28 00:00:53 +04:00
|
|
|
$parts = explode(' as ', $name);
|
|
|
|
if (count($parts) > 1) {
|
2007-11-18 19:06:37 +03:00
|
|
|
$fieldName = $parts[1];
|
|
|
|
} else {
|
|
|
|
$fieldName = $parts[0];
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
$name = strtolower($parts[0]);
|
2007-11-19 20:55:23 +03:00
|
|
|
if ($prepend) {
|
|
|
|
$this->_columnNames = array_merge(array($fieldName => $name), $this->_columnNames);
|
|
|
|
$this->_fieldNames = array_merge(array($name => $fieldName), $this->_fieldNames);
|
|
|
|
} else {
|
|
|
|
$this->_columnNames[$fieldName] = $name;
|
|
|
|
$this->_fieldNames[$name] = $fieldName;
|
|
|
|
}
|
2007-08-01 23:32:53 +04:00
|
|
|
|
|
|
|
if ($length == null) {
|
|
|
|
switch ($type) {
|
|
|
|
case 'string':
|
|
|
|
case 'clob':
|
2007-08-10 11:07:24 +04:00
|
|
|
case 'float':
|
2007-08-01 23:32:53 +04:00
|
|
|
case 'integer':
|
|
|
|
case 'array':
|
|
|
|
case 'object':
|
|
|
|
case 'blob':
|
|
|
|
case 'gzip':
|
|
|
|
// use php int max
|
|
|
|
$length = 2147483647;
|
|
|
|
break;
|
|
|
|
case 'boolean':
|
|
|
|
$length = 1;
|
|
|
|
case 'date':
|
|
|
|
// YYYY-MM-DD ISO 8601
|
|
|
|
$length = 10;
|
|
|
|
case 'time':
|
|
|
|
// HH:NN:SS+00:00 ISO 8601
|
|
|
|
$length = 14;
|
|
|
|
case 'timestamp':
|
|
|
|
// YYYY-MM-DDTHH:MM:SS+00:00 ISO 8601
|
|
|
|
$length = 25;
|
|
|
|
break;
|
|
|
|
}
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
|
|
|
|
$options['type'] = $type;
|
|
|
|
$options['length'] = $length;
|
|
|
|
|
|
|
|
if ($prepend) {
|
|
|
|
$this->_columns = array_merge(array($name => $options), $this->_columns);
|
|
|
|
} else {
|
|
|
|
$this->_columns[$name] = $options;
|
|
|
|
}
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
if ( ! empty($options['primary'])) {
|
2007-11-18 19:06:37 +03:00
|
|
|
if (isset($this->_identifier)) {
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->_identifier = $this->_identifier;
|
2007-11-18 19:06:37 +03:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
if ( ! in_array($fieldName, (array) $this->_identifier)) {
|
2007-11-18 19:06:37 +03:00
|
|
|
$this->_identifier[] = $fieldName;
|
|
|
|
}
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
|
|
|
if (isset($options['default'])) {
|
|
|
|
$this->hasDefaultValues = true;
|
|
|
|
}
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* hasDefaultValues
|
|
|
|
* returns true if this table has default values, otherwise false
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasDefaultValues()
|
|
|
|
{
|
|
|
|
return $this->hasDefaultValues;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getDefaultValueOf
|
|
|
|
* returns the default value(if any) for given column
|
|
|
|
*
|
2007-11-18 19:06:37 +03:00
|
|
|
* @param string $fieldName
|
2007-05-28 00:00:53 +04:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
public function getDefaultValueOf($fieldName)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
$columnName = $this->getColumnName($fieldName);
|
|
|
|
if ( ! isset($this->_columns[$columnName])) {
|
|
|
|
throw new Doctrine_Table_Exception("Couldn't get default value. Column ".$columnName." doesn't exist.");
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
if (isset($this->_columns[$columnName]['default'])) {
|
|
|
|
return $this->_columns[$columnName]['default'];
|
2007-05-28 00:00:53 +04:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getIdentifier()
|
|
|
|
{
|
2007-09-21 00:48:53 +04:00
|
|
|
return $this->_identifier;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
public function setIdentifier($identifier)
|
|
|
|
{
|
|
|
|
$this->_identifier = $identifier;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getIdentifierType()
|
|
|
|
{
|
2007-09-21 00:48:53 +04:00
|
|
|
return $this->_identifierType;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
public function setIdentifierType($type)
|
|
|
|
{
|
|
|
|
$this->_identifierType = $type;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* hasColumn
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
public function hasColumn($columnName)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
return isset($this->_columns[$columnName]);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-12-11 18:25:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* hasField
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasField($fieldName)
|
|
|
|
{
|
|
|
|
return isset($this->_columnNames[$fieldName]);
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2007-11-18 19:06:37 +03:00
|
|
|
* @param string $fieldName
|
2007-05-28 00:00:53 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
public function getEnumValues($fieldName)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
$columnName = $this->getColumnName($fieldName);
|
|
|
|
if (isset($this->_columns[$columnName]['values'])) {
|
|
|
|
return $this->_columns[$columnName]['values'];
|
2007-05-28 00:00:53 +04:00
|
|
|
} else {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* enumValue
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @param integer $index
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
public function enumValue($fieldName, $index)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-09-02 12:53:16 +04:00
|
|
|
if ($index instanceof Doctrine_Null) {
|
2007-05-28 00:00:53 +04:00
|
|
|
return $index;
|
2007-09-02 12:53:16 +04:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
|
|
|
|
$columnName = $this->getColumnName($fieldName);
|
2008-01-05 22:55:56 +03:00
|
|
|
if ( ! $this->_conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM) &&
|
|
|
|
isset($this->_columns[$columnName]['values'][$index])) {
|
2007-11-18 19:06:37 +03:00
|
|
|
return $this->_columns[$columnName]['values'][$index];
|
2007-09-02 12:53:16 +04:00
|
|
|
}
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-09-02 12:53:16 +04:00
|
|
|
return $index;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* enumIndex
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @param mixed $value
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
public function enumIndex($fieldName, $value)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
$values = $this->getEnumValues($fieldName);
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-09-02 12:53:16 +04:00
|
|
|
$index = array_search($value, $values);
|
2008-01-05 22:55:56 +03:00
|
|
|
if ($index === false || ! $this->_conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) {
|
2007-09-02 12:53:16 +04:00
|
|
|
return $index;
|
|
|
|
}
|
2007-09-07 17:27:02 +04:00
|
|
|
return $value;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getColumnCount
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
|
|
|
* @return integer the number of columns in this table
|
|
|
|
*/
|
2007-06-28 15:56:56 +04:00
|
|
|
public function getColumnCount()
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
|
|
|
return $this->columnCount;
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
public function setColumnCount($count)
|
|
|
|
{
|
|
|
|
$this->columnCount = $count;
|
|
|
|
}
|
2007-05-28 00:00:53 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns all columns and their definitions
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2007-06-28 15:56:56 +04:00
|
|
|
public function getColumns()
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-09-21 00:58:54 +04:00
|
|
|
return $this->_columns;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-06 01:18:40 +04:00
|
|
|
/**
|
|
|
|
* removeColumn
|
|
|
|
* removes given column
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2007-11-23 01:02:35 +03:00
|
|
|
public function removeColumn($fieldName)
|
2007-10-06 01:18:40 +04:00
|
|
|
{
|
2007-11-23 01:02:35 +03:00
|
|
|
$columnName = array_search($fieldName, $this->_fieldNames);
|
|
|
|
|
|
|
|
unset($this->_fieldNames[$columnName]);
|
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
if (isset($this->_columns[$columnName])) {
|
|
|
|
unset($this->_columns[$columnName]);
|
2007-10-06 01:18:40 +04:00
|
|
|
return true;
|
|
|
|
}
|
2007-11-23 01:02:35 +03:00
|
|
|
$this->columnCount--;
|
2007-10-06 01:18:40 +04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2007-11-18 19:06:37 +03:00
|
|
|
* returns an array containing all the column names.
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
public function getColumnNames(array $fieldNames = null)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
if ($fieldNames === null) {
|
|
|
|
return array_keys($this->_columns);
|
|
|
|
} else {
|
|
|
|
$columnNames = array();
|
|
|
|
foreach ($fieldNames as $fieldName) {
|
|
|
|
$columnNames[] = $this->getColumnName($fieldName);
|
|
|
|
}
|
|
|
|
return $columnNames;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns an array with all the identifier column names.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getIdentifierColumnNames()
|
|
|
|
{
|
|
|
|
return $this->getColumnNames((array) $this->getIdentifier());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns an array containing all the field names.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getFieldNames()
|
|
|
|
{
|
|
|
|
return array_values($this->_fieldNames);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getDefinitionOf
|
|
|
|
*
|
|
|
|
* @return mixed array on success, false on failure
|
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
public function getDefinitionOf($fieldName)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
$columnName = $this->getColumnName($fieldName);
|
|
|
|
return $this->getColumnDefinition($columnName);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getTypeOf
|
|
|
|
*
|
|
|
|
* @return mixed string on success, false on failure
|
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
public function getTypeOf($fieldName)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-12-02 18:04:51 +03:00
|
|
|
return $this->getTypeOfColumn($this->getColumnName($fieldName));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getTypeOfColumn
|
|
|
|
*
|
|
|
|
* @return mixed The column type or FALSE if the type cant be determined.
|
|
|
|
*/
|
|
|
|
public function getTypeOfColumn($columnName)
|
|
|
|
{
|
|
|
|
return isset($this->_columns[$columnName]) ? $this->_columns[$columnName]['type'] : false;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* getTableName
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
public function getTableName()
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
return $this->_options['tableName'];
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
public function bindRelation($args, $type)
|
|
|
|
{
|
|
|
|
return $this->bind($args, $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DESCRIBE WHAT THIS METHOD DOES, PLEASE!
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
2008-01-05 22:55:56 +03:00
|
|
|
* @todo Name proposal: addRelation
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
public function bind($args, $type)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
$options = array();
|
|
|
|
$options['type'] = $type;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
if ( ! isset($args[1])) {
|
|
|
|
$args[1] = array();
|
|
|
|
}
|
2007-07-05 23:04:37 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
// the following is needed for backwards compatibility
|
|
|
|
if (is_string($args[1])) {
|
|
|
|
if ( ! isset($args[2])) {
|
|
|
|
$args[2] = array();
|
|
|
|
} elseif (is_string($args[2])) {
|
|
|
|
$args[2] = (array) $args[2];
|
|
|
|
}
|
2007-07-05 23:04:37 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
$classes = array_merge($this->getOption('parents'), array($this->getComponentName()));
|
2007-07-05 23:04:37 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
$e = explode('.', $args[1]);
|
|
|
|
if (in_array($e[0], $classes)) {
|
|
|
|
if ($options['type'] >= Doctrine_Relation::MANY) {
|
|
|
|
$options['foreign'] = $e[1];
|
|
|
|
} else {
|
|
|
|
$options['local'] = $e[1];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$e2 = explode(' as ', $args[0]);
|
|
|
|
if ($e[0] !== $e2[0] && ( ! isset($e2[1]) || $e[0] !== $e2[1])) {
|
|
|
|
$options['refClass'] = $e[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$options['foreign'] = $e[1];
|
2007-07-05 23:04:37 +04:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
$options = array_merge($args[2], $options);
|
|
|
|
|
|
|
|
$this->_parser->bind($args[0], $options);
|
|
|
|
} else {
|
|
|
|
$options = array_merge($args[1], $options);
|
|
|
|
$this->_parser->bind($args[0], $options);
|
2007-07-05 23:04:37 +04:00
|
|
|
}
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* hasRelation
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
2008-01-05 22:55:56 +03:00
|
|
|
* @param string $alias the relation to check if exists
|
|
|
|
* @return boolean true if the relation exists otherwise false
|
2007-09-02 12:53:16 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
public function hasRelation($alias)
|
2007-10-21 11:24:18 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
return $this->_parser->hasRelation($alias);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 11:24:18 +04:00
|
|
|
|
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* getRelation
|
2007-10-21 11:24:18 +04:00
|
|
|
*
|
2008-01-05 22:55:56 +03:00
|
|
|
* @param string $alias relation alias
|
2007-10-21 11:24:18 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
public function getRelation($alias, $recursive = true)
|
|
|
|
{
|
|
|
|
return $this->_parser->getRelation($alias, $recursive);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRelationParser()
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
return $this->_parser;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 11:24:18 +04:00
|
|
|
|
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* getRelations
|
|
|
|
* returns an array containing all relation objects
|
2007-10-21 11:24:18 +04:00
|
|
|
*
|
2008-01-05 22:55:56 +03:00
|
|
|
* @return array an array of Doctrine_Relation objects
|
2007-10-21 11:24:18 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
public function getRelations()
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
return $this->_parser->getRelations();
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-10-21 11:24:18 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* getTemplates
|
|
|
|
* returns all templates attached to this table
|
2007-10-21 11:24:18 +04:00
|
|
|
*
|
2008-01-05 22:55:56 +03:00
|
|
|
* @return array an array containing all templates
|
2007-10-21 11:24:18 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
public function getTemplates()
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
return $this->_templates;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
public function getInheritanceType()
|
|
|
|
{
|
|
|
|
return $this->_inheritanceType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setInheritanceType($type)
|
|
|
|
{
|
|
|
|
$this->_inheritanceType = $type;
|
|
|
|
}
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* export
|
|
|
|
* exports this table to database based on column and option definitions
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
2008-01-05 22:55:56 +03:00
|
|
|
* @throws Doctrine_Connection_Exception if some error other than Doctrine::ERR_ALREADY_EXISTS
|
|
|
|
* occurred during the create table operation
|
|
|
|
* @return boolean whether or not the export operation was successful
|
|
|
|
* false if table already existed in the database
|
2007-09-02 12:53:16 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
public function export()
|
2007-10-21 11:24:18 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->_conn->export->exportTable($this);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-07-18 00:59:09 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
/**
|
|
|
|
* getExportableFormat
|
|
|
|
* Returns an array with the DDL for this table object.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @todo move to Table
|
|
|
|
*/
|
|
|
|
public function getExportableFormat($parseForeignKeys = true)
|
|
|
|
{
|
|
|
|
$columns = array();
|
|
|
|
$primary = array();
|
|
|
|
|
|
|
|
foreach ($this->getColumns() as $name => $definition) {
|
|
|
|
switch ($definition['type']) {
|
|
|
|
case 'enum':
|
|
|
|
if (isset($definition['default'])) {
|
|
|
|
$definition['default'] = $this->enumIndex($name, $definition['default']);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'boolean':
|
|
|
|
if (isset($definition['default'])) {
|
|
|
|
$definition['default'] = $this->_conn->convertBooleans($definition['default']);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$columns[$name] = $definition;
|
|
|
|
|
|
|
|
if (isset($definition['primary']) && $definition['primary']) {
|
|
|
|
$primary[] = $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$options['foreignKeys'] = array();
|
|
|
|
|
|
|
|
if ($parseForeignKeys && $this->getAttribute(Doctrine::ATTR_EXPORT)
|
|
|
|
& Doctrine::EXPORT_CONSTRAINTS) {
|
|
|
|
|
|
|
|
$constraints = array();
|
|
|
|
|
|
|
|
$emptyIntegrity = array('onUpdate' => null,
|
|
|
|
'onDelete' => null);
|
|
|
|
|
|
|
|
foreach ($this->getRelations() as $name => $relation) {
|
|
|
|
$fk = $relation->toArray();
|
|
|
|
$fk['foreignTable'] = $relation->getTable()->getTableName();
|
|
|
|
|
|
|
|
if ($relation->getTable() === $this && in_array($relation->getLocal(), $primary)) {
|
|
|
|
if ($relation->hasConstraint()) {
|
|
|
|
throw new Doctrine_Table_Exception("Badly constructed integrity constraints.");
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$integrity = array('onUpdate' => $fk['onUpdate'],
|
|
|
|
'onDelete' => $fk['onDelete']);
|
|
|
|
|
|
|
|
if ($relation instanceof Doctrine_Relation_LocalKey) {
|
|
|
|
$def = array('local' => $relation->getLocal(),
|
|
|
|
'foreign' => $relation->getForeign(),
|
|
|
|
'foreignTable' => $relation->getTable()->getTableName());
|
|
|
|
|
|
|
|
if (($key = array_search($def, $options['foreignKeys'])) === false) {
|
|
|
|
$options['foreignKeys'][] = $def;
|
|
|
|
$constraints[] = $integrity;
|
|
|
|
} else {
|
|
|
|
if ($integrity !== $emptyIntegrity) {
|
|
|
|
$constraints[$key] = $integrity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($constraints as $k => $def) {
|
|
|
|
$options['foreignKeys'][$k] = array_merge($options['foreignKeys'][$k], $def);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$options['primary'] = $primary;
|
|
|
|
|
|
|
|
return array('tableName' => $this->getOption('tableName'),
|
|
|
|
'columns' => $columns,
|
|
|
|
'options' => array_merge($this->getOptions(), $options));
|
|
|
|
}
|
|
|
|
|
2007-10-21 11:24:18 +04:00
|
|
|
/**
|
|
|
|
* getTemplate
|
|
|
|
*
|
|
|
|
* @param string $template
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-07-18 00:59:09 +04:00
|
|
|
public function getTemplate($template)
|
2007-06-08 23:27:38 +04:00
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! isset($this->_templates[$template])) {
|
2007-07-18 00:59:09 +04:00
|
|
|
throw new Doctrine_Table_Exception('Template ' . $template . ' not loaded');
|
2007-09-03 18:57:18 +04:00
|
|
|
}
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-09-03 18:57:18 +04:00
|
|
|
return $this->_templates[$template];
|
2007-07-18 00:59:09 +04:00
|
|
|
}
|
2007-10-09 01:22:13 +04:00
|
|
|
|
|
|
|
public function hasTemplate($template)
|
|
|
|
{
|
|
|
|
return isset($this->_templates[$template]);
|
|
|
|
}
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-07-18 00:59:09 +04:00
|
|
|
public function addTemplate($template, Doctrine_Template $impl)
|
|
|
|
{
|
|
|
|
$this->_templates[$template] = $impl;
|
2007-09-21 17:48:31 +04:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-11-29 01:56:45 +03:00
|
|
|
public function getGenerators()
|
2007-11-27 19:08:34 +03:00
|
|
|
{
|
2007-11-29 01:56:45 +03:00
|
|
|
return $this->_generators;
|
2007-11-27 19:08:34 +03:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-11-29 01:56:45 +03:00
|
|
|
public function getGenerator($generator)
|
2007-11-27 01:25:18 +03:00
|
|
|
{
|
2007-11-29 01:56:45 +03:00
|
|
|
if ( ! isset($this->_generators[$generator])) {
|
|
|
|
throw new Doctrine_Table_Exception('Generator ' . $generator . ' not loaded');
|
2007-11-27 01:25:18 +03:00
|
|
|
}
|
|
|
|
|
2007-11-29 01:56:45 +03:00
|
|
|
return $this->_generators[$plugin];
|
2007-11-27 01:25:18 +03:00
|
|
|
}
|
|
|
|
|
2007-11-29 01:56:45 +03:00
|
|
|
public function hasGenerator($generator)
|
2007-11-27 01:25:18 +03:00
|
|
|
{
|
2007-11-29 01:56:45 +03:00
|
|
|
return isset($this->_generators[$generator]);
|
2007-11-27 01:25:18 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-11-29 01:56:45 +03:00
|
|
|
public function addGenerator(Doctrine_Record_Generator $generator, $name = null)
|
2007-11-27 01:25:18 +03:00
|
|
|
{
|
|
|
|
if ($name === null) {
|
2007-11-29 01:56:45 +03:00
|
|
|
$this->_generators[] = $generator;
|
2007-11-27 01:25:18 +03:00
|
|
|
} else {
|
2007-11-29 01:56:45 +03:00
|
|
|
$this->_generators[$name] = $generator;
|
2007-11-27 01:25:18 +03:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* unshiftFilter
|
|
|
|
*
|
|
|
|
* @param object Doctrine_Record_Filter $filter
|
|
|
|
* @return object $this
|
|
|
|
*/
|
|
|
|
public function unshiftFilter(Doctrine_Record_Filter $filter)
|
|
|
|
{
|
|
|
|
$filter->setTable($this);
|
|
|
|
$filter->init();
|
|
|
|
array_unshift($this->_filters, $filter);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getTree
|
|
|
|
*
|
|
|
|
* getter for associated tree
|
|
|
|
*
|
|
|
|
* @return mixed if tree return instance of Doctrine_Tree, otherwise returns false
|
|
|
|
*/
|
|
|
|
public function getTree()
|
|
|
|
{
|
|
|
|
if ($this->getOption('treeImpl')) {
|
|
|
|
if ( ! $this->_tree) {
|
|
|
|
$options = $this->getOption('treeOptions') ? $this->getOption('treeOptions') : array();
|
|
|
|
$this->_tree = Doctrine_Tree::factory($this,
|
|
|
|
$this->getOption('treeImpl'), $options);
|
|
|
|
}
|
|
|
|
return $this->_tree;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* isTree
|
|
|
|
*
|
|
|
|
* determine if table acts as tree
|
|
|
|
*
|
|
|
|
* @return mixed if tree return true, otherwise returns false
|
|
|
|
*/
|
|
|
|
public function isTree()
|
|
|
|
{
|
|
|
|
return ( ! is_null($this->getOption('treeImpl'))) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getFilters
|
|
|
|
*
|
|
|
|
* @return array $filters
|
|
|
|
*/
|
|
|
|
public function getFilters()
|
|
|
|
{
|
|
|
|
return $this->_filters;
|
|
|
|
}
|
|
|
|
|
2007-10-06 00:29:33 +04:00
|
|
|
/**
|
|
|
|
* bindQueryParts
|
|
|
|
* binds query parts to given component
|
|
|
|
*
|
|
|
|
* @param array $queryParts an array of pre-bound query parts
|
|
|
|
* @return Doctrine_Record this object
|
|
|
|
*/
|
|
|
|
public function bindQueryParts(array $queryParts)
|
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
$this->_options['queryParts'] = $queryParts;
|
2007-10-06 00:29:33 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-06 01:18:40 +04:00
|
|
|
/**
|
|
|
|
* bindQueryPart
|
|
|
|
* binds given value to given query part
|
|
|
|
*
|
|
|
|
* @param string $queryPart
|
|
|
|
* @param mixed $value
|
|
|
|
* @return Doctrine_Record this object
|
|
|
|
*/
|
|
|
|
public function bindQueryPart($queryPart, $value)
|
|
|
|
{
|
|
|
|
$this->_options['queryParts'][$queryPart] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 11:24:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getBoundQueryPart
|
|
|
|
*
|
|
|
|
* @param string $queryPart
|
|
|
|
* @return string $queryPart
|
|
|
|
*/
|
2007-10-06 01:18:40 +04:00
|
|
|
public function getBoundQueryPart($queryPart)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_options['queryParts'][$queryPart])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->_options['queryParts'][$queryPart];
|
|
|
|
}
|
2007-10-02 15:28:26 +04:00
|
|
|
|
2007-10-21 11:24:18 +04:00
|
|
|
/**
|
2008-01-05 22:55:56 +03:00
|
|
|
* setTableName
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
2008-01-05 22:55:56 +03:00
|
|
|
* @param string $tableName
|
|
|
|
* @return void
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2008-01-05 22:55:56 +03:00
|
|
|
public function setTableName($tableName)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->setOption('tableName', $this->_conn->formatter->getTableName($tableName));
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 11:24:18 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
public function serialize()
|
2007-10-21 11:24:18 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
return serialize($this->_columns);
|
2007-10-21 11:24:18 +04:00
|
|
|
}
|
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
public function unserialize($serialized)
|
2007-10-21 11:24:18 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
return true;
|
2007-10-21 11:24:18 +04:00
|
|
|
}
|
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
public function __toString()
|
2007-10-21 11:24:18 +04:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
return spl_object_hash($this);
|
2007-10-21 11:24:18 +04:00
|
|
|
}
|
2007-10-22 21:33:47 +04:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|