2007-05-28 00:00:53 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* Doctrine_Table represents a database table
|
|
|
|
* each Doctrine_Table holds the information of foreignKeys and associations
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Table
|
2007-05-28 00:00:53 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @version $Revision$
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
|
|
|
class Doctrine_Table extends Doctrine_Configurable implements Countable
|
|
|
|
{
|
|
|
|
/**
|
2007-11-09 02:03:28 +03:00
|
|
|
* @var array $data temporary data which is then loaded into Doctrine_Record::$_data
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2007-10-16 01:39:35 +04:00
|
|
|
protected $_data = 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 mixed $identifier The field names of all fields that are part of the identifier/primary key
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
protected $_identifier = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @see Doctrine_Identifier constants
|
|
|
|
* @var integer $identifierType the type of identifier this table uses
|
|
|
|
*/
|
2007-10-16 01:39:35 +04:00
|
|
|
protected $_identifierType;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var Doctrine_Connection $conn Doctrine_Connection object that created this table
|
|
|
|
*/
|
2007-10-16 01:39:35 +04:00
|
|
|
protected $_conn;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var array $identityMap first level cache
|
|
|
|
*/
|
2007-10-16 01:39:35 +04:00
|
|
|
protected $_identityMap = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var Doctrine_Table_Repository $repository record repository
|
|
|
|
*/
|
2007-10-16 01:39:35 +04:00
|
|
|
protected $_repository;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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
|
|
|
*/
|
2007-09-21 00:58:54 +04: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
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
protected $_fieldNames = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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;
|
2007-10-21 10:23:59 +04: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;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* -- inheritanceMap inheritanceMap is used for inheritance mapping, keys representing columns and values
|
|
|
|
* the column values that should correspond to child classes
|
|
|
|
*
|
|
|
|
* -- 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
|
|
|
*/
|
2007-10-06 01:18:40 +04:00
|
|
|
protected $_options = array('name' => null,
|
|
|
|
'tableName' => null,
|
|
|
|
'sequenceName' => null,
|
|
|
|
'inheritanceMap' => array(),
|
|
|
|
'enumMap' => array(),
|
|
|
|
'type' => null,
|
|
|
|
'charset' => null,
|
|
|
|
'collation' => null,
|
|
|
|
'treeImpl' => null,
|
|
|
|
'treeOptions' => null,
|
|
|
|
'indexes' => array(),
|
|
|
|
'parents' => array(),
|
|
|
|
'queryParts' => array(),
|
|
|
|
'versioning' => null,
|
|
|
|
);
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var Doctrine_Tree $tree tree object associated with this table
|
|
|
|
*/
|
2007-09-21 00:53:15 +04:00
|
|
|
protected $_tree;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* @var Doctrine_Relation_Parser $_parser relation parser object
|
|
|
|
*/
|
|
|
|
protected $_parser;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-08 23:27:38 +04:00
|
|
|
/**
|
2007-07-18 00:59:09 +04:00
|
|
|
* @var array $_templates an array containing all templates attached to this table
|
2007-06-08 23:27:38 +04:00
|
|
|
*/
|
2007-09-21 17:48:31 +04:00
|
|
|
protected $_templates = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-09-21 17:48:31 +04:00
|
|
|
/**
|
|
|
|
* @var array $_filters an array containing all record filters attached to this table
|
|
|
|
*/
|
|
|
|
protected $_filters = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-16 01:39:35 +04:00
|
|
|
/**
|
|
|
|
* @var array $_invokedMethods method invoker cache
|
|
|
|
*/
|
2007-10-08 03:36:28 +04:00
|
|
|
protected $_invokedMethods = array();
|
2007-09-21 17:48:31 +04:00
|
|
|
|
2007-07-18 00:59:09 +04:00
|
|
|
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* the constructor
|
2007-10-16 01:39:35 +04:00
|
|
|
*
|
2007-05-28 00:00:53 +04:00
|
|
|
* @throws Doctrine_Connection_Exception if there are no opened connections
|
2007-10-16 01:39:35 +04:00
|
|
|
* @param string $name the name of the component
|
|
|
|
* @param Doctrine_Connection $conn the connection associated with this table
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2007-10-16 02:01:04 +04:00
|
|
|
public function __construct($name, Doctrine_Connection $conn, $initDefinition = false)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-09-21 00:21:08 +04:00
|
|
|
$this->_conn = $conn;
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-09-21 00:21:08 +04:00
|
|
|
$this->setParent($this->_conn);
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-10-06 01:18:40 +04:00
|
|
|
$this->_options['name'] = $name;
|
2007-05-28 00:00:53 +04:00
|
|
|
$this->_parser = new Doctrine_Relation_Parser($this);
|
|
|
|
|
2007-10-16 02:01:04 +04:00
|
|
|
if ($initDefinition) {
|
2007-11-18 19:06:37 +03:00
|
|
|
$record = $this->initDefinition();
|
2007-10-16 02:01:04 +04:00
|
|
|
|
|
|
|
$this->initIdentifier();
|
|
|
|
|
|
|
|
$record->setUp();
|
|
|
|
|
|
|
|
// if tree, set up tree
|
|
|
|
if ($this->isTree()) {
|
|
|
|
$this->getTree()->setUp();
|
|
|
|
}
|
2007-10-16 01:39:35 +04:00
|
|
|
}
|
|
|
|
$this->_filters[] = new Doctrine_Record_Filter_Standard();
|
|
|
|
$this->_repository = new Doctrine_Table_Repository($this);
|
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the in-memory table definition.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
public function initDefinition()
|
|
|
|
{
|
|
|
|
$name = $this->_options['name'];
|
2007-05-28 00:00:53 +04:00
|
|
|
if ( ! class_exists($name) || empty($name)) {
|
|
|
|
throw new Doctrine_Exception("Couldn't find class " . $name);
|
|
|
|
}
|
|
|
|
$record = new $name($this);
|
|
|
|
|
|
|
|
$names = array();
|
|
|
|
|
|
|
|
$class = $name;
|
|
|
|
|
|
|
|
// get parent classes
|
|
|
|
|
|
|
|
do {
|
2007-09-01 19:51:46 +04:00
|
|
|
if ($class === 'Doctrine_Record') {
|
2007-09-02 12:53:16 +04:00
|
|
|
break;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
|
|
|
|
2007-10-13 01:32:26 +04:00
|
|
|
$name = $class;
|
2007-05-28 00:00:53 +04:00
|
|
|
$names[] = $name;
|
|
|
|
} while ($class = get_parent_class($class));
|
|
|
|
|
2007-10-13 01:32:26 +04:00
|
|
|
if ($class === false) {
|
|
|
|
throw new Doctrine_Table_Exception('Unknown component.');
|
|
|
|
}
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
// reverse names
|
|
|
|
$names = array_reverse($names);
|
|
|
|
// save parents
|
|
|
|
array_pop($names);
|
2007-10-06 01:18:40 +04:00
|
|
|
$this->_options['parents'] = $names;
|
2007-05-28 00:00:53 +04:00
|
|
|
|
|
|
|
// create database table
|
|
|
|
if (method_exists($record, 'setTableDefinition')) {
|
|
|
|
$record->setTableDefinition();
|
2007-08-14 01:39:29 +04:00
|
|
|
// get the declaring class of setTableDefinition method
|
2007-10-06 01:18:40 +04:00
|
|
|
$method = new ReflectionMethod($this->_options['name'], 'setTableDefinition');
|
2007-11-18 19:06:37 +03:00
|
|
|
$class = $method->getDeclaringClass();
|
|
|
|
|
2007-08-14 01:39:29 +04:00
|
|
|
} else {
|
|
|
|
$class = new ReflectionClass($class);
|
|
|
|
}
|
2007-11-09 02:03:28 +03:00
|
|
|
|
|
|
|
$this->_options['joinedParents'] = array();
|
|
|
|
|
|
|
|
foreach (array_reverse($this->_options['parents']) as $parent) {
|
2007-11-18 19:06:37 +03:00
|
|
|
|
2007-11-09 02:03:28 +03:00
|
|
|
if ($parent === $class->getName()) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-11-10 16:21:40 +03:00
|
|
|
$ref = new ReflectionClass($parent);
|
|
|
|
|
|
|
|
if ($ref->isAbstract()) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
$parentTable = $this->_conn->getTable($parent);
|
2007-11-09 02:03:28 +03:00
|
|
|
|
|
|
|
$found = false;
|
2007-11-18 19:06:37 +03:00
|
|
|
$parentColumns = $parentTable->getColumns();
|
2007-11-09 02:03:28 +03:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
foreach ($parentColumns as $columnName => $definition) {
|
2007-11-09 02:03:28 +03:00
|
|
|
if ( ! isset($definition['primary'])) {
|
2007-11-18 19:06:37 +03:00
|
|
|
if (isset($this->_columns[$columnName])) {
|
2007-11-09 02:03:28 +03:00
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
} else {
|
2007-11-18 19:06:37 +03:00
|
|
|
if ( ! isset($parentColumns[$columnName]['owner'])) {
|
|
|
|
$parentColumns[$columnName]['owner'] = $parentTable->getComponentName();
|
2007-11-09 02:03:28 +03:00
|
|
|
}
|
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
$this->_options['joinedParents'][] = $parentColumns[$columnName]['owner'];
|
2007-11-09 02:03:28 +03:00
|
|
|
}
|
|
|
|
} else {
|
2007-11-18 19:06:37 +03:00
|
|
|
unset($parentColumns[$columnName]);
|
2007-11-09 02:03:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($found) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
foreach ($parentColumns as $columnName => $definition) {
|
|
|
|
$fullName = $columnName . ' as ' . $parentTable->getFieldName($columnName);
|
|
|
|
$this->setColumn($fullName, $definition['type'], $definition['length'], $definition, true);
|
|
|
|
}
|
2007-11-09 02:03:28 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_options['joinedParents'] = array_values(array_unique($this->_options['joinedParents']));
|
|
|
|
|
2007-10-06 01:18:40 +04:00
|
|
|
$this->_options['declaringClass'] = $class;
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-08-14 01:39:29 +04:00
|
|
|
// set the table definition for the given tree implementation
|
|
|
|
if ($this->isTree()) {
|
|
|
|
$this->getTree()->setTableDefinition();
|
|
|
|
}
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-09-21 00:58:54 +04:00
|
|
|
$this->columnCount = count($this->_columns);
|
2007-10-16 01:39:35 +04:00
|
|
|
|
2007-10-06 01:18:40 +04:00
|
|
|
if ( ! isset($this->_options['tableName'])) {
|
|
|
|
$this->_options['tableName'] = Doctrine::tableize($class->getName());
|
2007-08-14 01:39:29 +04:00
|
|
|
}
|
2007-10-16 00:43:24 +04:00
|
|
|
|
2007-10-16 01:39:35 +04:00
|
|
|
return $record;
|
2007-10-16 00:43:24 +04:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the table identifier(s)/primary key(s)
|
|
|
|
*
|
|
|
|
*/
|
2007-10-16 00:43:24 +04:00
|
|
|
public function initIdentifier()
|
|
|
|
{
|
2007-09-21 00:58:54 +04:00
|
|
|
switch (count($this->_identifier)) {
|
2007-08-14 01:39:29 +04:00
|
|
|
case 0:
|
2007-11-09 02:03:28 +03:00
|
|
|
if ( ! empty($this->_options['joinedParents'])) {
|
|
|
|
$root = current($this->_options['joinedParents']);
|
|
|
|
|
|
|
|
$table = $this->_conn->getTable($root);
|
|
|
|
|
|
|
|
$this->_identifier = $table->getIdentifier();
|
|
|
|
|
|
|
|
$this->_identifierType = ($table->getIdentifierType() !== Doctrine::IDENTIFIER_AUTOINC)
|
|
|
|
? $table->getIdentifierType() : Doctrine::IDENTIFIER_NATURAL;
|
|
|
|
|
|
|
|
// add all inherited primary keys
|
|
|
|
foreach ((array) $this->_identifier as $id) {
|
|
|
|
$definition = $table->getDefinitionOf($id);
|
|
|
|
|
|
|
|
// inherited primary keys shouldn't contain autoinc
|
|
|
|
// and sequence definitions
|
|
|
|
unset($definition['autoincrement']);
|
|
|
|
unset($definition['sequence']);
|
|
|
|
|
|
|
|
// add the inherited primary key column
|
2007-11-18 19:06:37 +03:00
|
|
|
$fullName = $id . ' as ' . $table->getFieldName($id);
|
|
|
|
$this->setColumn($fullName, $definition['type'], $definition['length'],
|
|
|
|
$definition, true);
|
2007-11-09 02:03:28 +03:00
|
|
|
}
|
|
|
|
} else {
|
2007-11-18 19:06:37 +03:00
|
|
|
$definition = array('type' => 'integer',
|
|
|
|
'length' => 20,
|
|
|
|
'autoincrement' => true,
|
|
|
|
'primary' => true);
|
|
|
|
$this->setColumn('id', $definition['type'], $definition['length'], $definition, true);
|
2007-11-09 02:03:28 +03:00
|
|
|
$this->_identifier = 'id';
|
|
|
|
$this->_identifierType = Doctrine::IDENTIFIER_AUTOINC;
|
|
|
|
}
|
2007-08-14 01:39:29 +04:00
|
|
|
$this->columnCount++;
|
|
|
|
break;
|
2007-09-21 00:53:15 +04:00
|
|
|
case 1:
|
2007-09-21 00:58:54 +04:00
|
|
|
foreach ($this->_identifier as $pk) {
|
2007-11-18 19:06:37 +03:00
|
|
|
$columnName = $this->getColumnName($pk);
|
|
|
|
$e = $this->_columns[$columnName];
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-09-21 00:53:15 +04:00
|
|
|
$found = false;
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-09-21 00:53:15 +04:00
|
|
|
foreach ($e as $option => $value) {
|
2007-10-16 00:43:24 +04:00
|
|
|
if ($found) {
|
|
|
|
break;
|
|
|
|
}
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-09-21 00:53:15 +04:00
|
|
|
$e2 = explode(':', $option);
|
|
|
|
|
|
|
|
switch (strtolower($e2[0])) {
|
|
|
|
case 'autoincrement':
|
|
|
|
case 'autoinc':
|
|
|
|
$this->_identifierType = Doctrine::IDENTIFIER_AUTOINC;
|
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
case 'seq':
|
|
|
|
case 'sequence':
|
|
|
|
$this->_identifierType = Doctrine::IDENTIFIER_SEQUENCE;
|
|
|
|
$found = true;
|
|
|
|
|
|
|
|
if ($value) {
|
2007-10-06 01:18:40 +04:00
|
|
|
$this->_options['sequenceName'] = $value;
|
2007-09-21 00:53:15 +04:00
|
|
|
} else {
|
|
|
|
if (($sequence = $this->getAttribute(Doctrine::ATTR_DEFAULT_SEQUENCE)) !== null) {
|
2007-10-06 01:18:40 +04:00
|
|
|
$this->_options['sequenceName'] = $sequence;
|
2007-08-14 01:39:29 +04:00
|
|
|
} else {
|
2007-10-06 01:18:40 +04:00
|
|
|
$this->_options['sequenceName'] = $this->_conn->getSequenceName($this->_options['tableName']);
|
2007-08-14 01:39:29 +04:00
|
|
|
}
|
2007-09-21 00:53:15 +04:00
|
|
|
}
|
|
|
|
break;
|
2007-08-14 01:39:29 +04:00
|
|
|
}
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-09-21 00:53:15 +04:00
|
|
|
if ( ! isset($this->_identifierType)) {
|
|
|
|
$this->_identifierType = Doctrine::IDENTIFIER_NATURAL;
|
|
|
|
}
|
|
|
|
}
|
2007-09-21 00:58:54 +04:00
|
|
|
|
|
|
|
$this->_identifier = $pk;
|
|
|
|
|
2007-09-21 00:53:15 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->_identifierType = Doctrine::IDENTIFIER_COMPOSITE;
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
public function getMethodOwner($method)
|
2007-10-08 03:36:28 +04:00
|
|
|
{
|
|
|
|
return (isset($this->_invokedMethods[$method])) ?
|
|
|
|
$this->_invokedMethods[$method] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setMethodOwner($method, $class)
|
|
|
|
{
|
|
|
|
$this->_invokedMethods[$method] = $class;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-08-27 19:04:32 +04:00
|
|
|
/**
|
|
|
|
* getTemplates
|
|
|
|
* returns all templates attached to this table
|
|
|
|
*
|
|
|
|
* @return array an array containing all templates
|
|
|
|
*/
|
|
|
|
public function getTemplates()
|
|
|
|
{
|
|
|
|
return $this->_templates;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* export
|
|
|
|
* exports this table to database based on column and option definitions
|
|
|
|
*
|
|
|
|
* @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
|
|
|
public function export()
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-09-21 00:21:08 +04:00
|
|
|
$this->_conn->export->exportTable($this);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-06 01:00:39 +04:00
|
|
|
/**
|
|
|
|
* getExportableFormat
|
|
|
|
* returns exportable presentation of this object
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2007-06-07 21:04:56 +04:00
|
|
|
public function getExportableFormat($parseForeignKeys = true)
|
2007-06-06 01:00:39 +04:00
|
|
|
{
|
|
|
|
$columns = array();
|
|
|
|
$primary = array();
|
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
foreach ($this->getColumns() as $name => $definition) {
|
2007-06-06 01:00:39 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
if (isset($definition['owner'])) {
|
2007-11-09 02:03:28 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-06-06 01:00:39 +04:00
|
|
|
switch ($definition['type']) {
|
2007-06-28 15:56:56 +04:00
|
|
|
case 'enum':
|
|
|
|
if (isset($definition['default'])) {
|
|
|
|
$definition['default'] = $this->enumIndex($name, $definition['default']);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'boolean':
|
|
|
|
if (isset($definition['default'])) {
|
|
|
|
$definition['default'] = $this->getConnection()->convertBooleans($definition['default']);
|
|
|
|
}
|
|
|
|
break;
|
2007-06-06 01:00:39 +04:00
|
|
|
}
|
|
|
|
$columns[$name] = $definition;
|
|
|
|
|
2007-09-03 18:57:18 +04:00
|
|
|
if (isset($definition['primary']) && $definition['primary']) {
|
2007-06-06 01:00:39 +04:00
|
|
|
$primary[] = $name;
|
|
|
|
}
|
|
|
|
}
|
2007-06-15 00:43:04 +04:00
|
|
|
$options['foreignKeys'] = array();
|
2007-06-06 01:00:39 +04:00
|
|
|
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
if ($parseForeignKeys && $this->getAttribute(Doctrine::ATTR_EXPORT)
|
|
|
|
& Doctrine::EXPORT_CONSTRAINTS) {
|
2007-06-15 00:43:04 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
$constraints = array();
|
2007-06-15 00:43:04 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
$emptyIntegrity = array('onUpdate' => null,
|
|
|
|
'onDelete' => null);
|
2007-06-15 00:43:04 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
foreach ($this->getRelations() as $name => $relation) {
|
|
|
|
$fk = $relation->toArray();
|
|
|
|
$fk['foreignTable'] = $relation->getTable()->getTableName();
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
if ($relation->getTable() === $this && in_array($relation->getLocal(), $primary)) {
|
|
|
|
if ($relation->hasConstraint()) {
|
|
|
|
throw new Doctrine_Table_Exception("Badly constructed integrity constraints.");
|
2007-06-07 21:04:56 +04:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
2007-06-15 00:43:04 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
$integrity = array('onUpdate' => $fk['onUpdate'],
|
|
|
|
'onDelete' => $fk['onDelete']);
|
2007-06-15 00:43:04 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
if ($relation instanceof Doctrine_Relation_LocalKey) {
|
|
|
|
$def = array('local' => $relation->getLocal(),
|
|
|
|
'foreign' => $relation->getForeign(),
|
|
|
|
'foreignTable' => $relation->getTable()->getTableName());
|
2007-06-15 00:43:04 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
if (($key = array_search($def, $options['foreignKeys'])) === false) {
|
|
|
|
$options['foreignKeys'][] = $def;
|
|
|
|
$constraints[] = $integrity;
|
|
|
|
} else {
|
|
|
|
if ($integrity !== $emptyIntegrity) {
|
|
|
|
$constraints[$key] = $integrity;
|
2007-06-15 00:43:04 +04:00
|
|
|
}
|
2007-06-07 21:04:56 +04:00
|
|
|
}
|
2007-06-15 00:43:04 +04:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
}
|
2007-06-15 00:43:04 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
foreach ($constraints as $k => $def) {
|
|
|
|
$options['foreignKeys'][$k] = array_merge($options['foreignKeys'][$k], $def);
|
2007-06-06 01:00:39 +04:00
|
|
|
}
|
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
|
2007-06-15 00:43:04 +04:00
|
|
|
$options['primary'] = $primary;
|
2007-09-02 12:53:16 +04:00
|
|
|
|
|
|
|
return array('tableName' => $this->getOption('tableName'),
|
|
|
|
'columns' => $columns,
|
2007-06-06 01:02:21 +04:00
|
|
|
'options' => array_merge($this->getOptions(), $options));
|
2007-06-06 01:00:39 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* exportConstraints
|
|
|
|
* exports the constraints of this table into database based on option definitions
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if something went wrong on db level
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function exportConstraints()
|
|
|
|
{
|
|
|
|
try {
|
2007-09-21 00:21:08 +04:00
|
|
|
$this->_conn->beginTransaction();
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-10-06 01:18:40 +04:00
|
|
|
foreach ($this->_options['index'] as $index => $definition) {
|
|
|
|
$this->_conn->export->createIndex($this->_options['tableName'], $index, $definition);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-09-21 00:21:08 +04:00
|
|
|
$this->_conn->commit();
|
2007-11-18 19:06:37 +03:00
|
|
|
} catch (Doctrine_Connection_Exception $e) {
|
2007-09-21 00:21:08 +04:00
|
|
|
$this->_conn->rollback();
|
2007-05-28 00:00:53 +04:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getRelationParser
|
|
|
|
* return the relation parser associated with this table
|
|
|
|
*
|
|
|
|
* @return Doctrine_Relation_Parser relation parser object
|
|
|
|
*/
|
|
|
|
public function getRelationParser()
|
|
|
|
{
|
|
|
|
return $this->_parser;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* __get
|
|
|
|
* an alias for getOption
|
|
|
|
*
|
|
|
|
* @param string $option
|
|
|
|
*/
|
|
|
|
public function __get($option)
|
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
if (isset($this->_options[$option])) {
|
|
|
|
return $this->_options[$option];
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* __isset
|
|
|
|
*
|
|
|
|
* @param string $option
|
|
|
|
*/
|
2007-09-02 12:53:16 +04:00
|
|
|
public function __isset($option)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
return isset($this->_options[$option]);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-06 00:55:54 +04:00
|
|
|
/**
|
|
|
|
* getOptions
|
|
|
|
* returns all options of this table and the associated values
|
|
|
|
*
|
|
|
|
* @return array all options and their values
|
|
|
|
*/
|
|
|
|
public function getOptions()
|
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
return $this->_options;
|
2007-06-06 00:55:54 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* addForeignKey
|
|
|
|
*
|
|
|
|
* adds a foreignKey to this table
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addForeignKey(array $definition)
|
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
$this->_options['foreignKeys'][] = $definition;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-24 03:18:11 +04:00
|
|
|
/**
|
|
|
|
* addCheckConstraint
|
2007-09-02 12:53:16 +04:00
|
|
|
*
|
2007-07-24 03:18:11 +04:00
|
|
|
* adds a check constraint to this table
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addCheckConstraint($definition, $name)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if (is_string($name)) {
|
2007-10-06 01:18:40 +04:00
|
|
|
$this->_options['checks'][$name] = $definition;
|
2007-07-24 03:18:11 +04:00
|
|
|
} else {
|
2007-10-06 01:18:40 +04:00
|
|
|
$this->_options['checks'][] = $definition;
|
2007-07-24 03:18:11 +04:00
|
|
|
}
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-07-24 03:18:11 +04:00
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* addIndex
|
2007-09-02 12:53:16 +04:00
|
|
|
*
|
2007-05-28 00:00:53 +04:00
|
|
|
* adds an index to this table
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addIndex($index, array $definition)
|
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
$this->_options['indexes'][$index] = $definition;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getIndex
|
|
|
|
*
|
|
|
|
* @return array|boolean array on success, FALSE on failure
|
|
|
|
*/
|
2007-09-02 12:53:16 +04:00
|
|
|
public function getIndex($index)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-10-06 01:18:40 +04: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
|
|
|
|
|
|
|
/**
|
|
|
|
* DESCRIBE WHAT THIS METHOD DOES, PLEASE!
|
|
|
|
*
|
|
|
|
* @todo Name proposal: addRelation
|
|
|
|
*/
|
2007-05-28 00:00:53 +04:00
|
|
|
public function bind($args, $type)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$options = array();
|
2007-05-28 00:00:53 +04:00
|
|
|
$options['type'] = $type;
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-06-27 20:59:23 +04:00
|
|
|
if ( ! isset($args[1])) {
|
|
|
|
$args[1] = array();
|
|
|
|
}
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-05-28 00:00:53 +04: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-10-06 01:18:40 +04:00
|
|
|
$classes = array_merge($this->_options['parents'], array($this->getComponentName()));
|
2007-05-28 00:00:53 +04:00
|
|
|
|
|
|
|
|
|
|
|
$e = explode('.', $args[1]);
|
|
|
|
if (in_array($e[0], $classes)) {
|
|
|
|
if ($options['type'] >= Doctrine_Relation::MANY) {
|
2007-09-02 12:53:16 +04:00
|
|
|
$options['foreign'] = $e[1];
|
2007-05-28 00:00:53 +04:00
|
|
|
} 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];
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = array_merge($args[2], $options);
|
|
|
|
|
|
|
|
$this->_parser->bind($args[0], $options);
|
2007-09-02 12:53:16 +04:00
|
|
|
} else {
|
2007-05-28 00:00:53 +04:00
|
|
|
$options = array_merge($args[1], $options);
|
|
|
|
$this->_parser->bind($args[0], $options);
|
|
|
|
}
|
|
|
|
}
|
2007-07-21 19:46:31 +04:00
|
|
|
|
2007-09-02 12:53:16 +04:00
|
|
|
/**
|
2007-07-21 19:46:31 +04:00
|
|
|
* hasRelation
|
|
|
|
*
|
|
|
|
* @param string $alias the relation to check if exists
|
|
|
|
* @return boolean true if the relation exists otherwise false
|
|
|
|
*/
|
|
|
|
public function hasRelation($alias)
|
|
|
|
{
|
|
|
|
return $this->_parser->hasRelation($alias);
|
2007-09-02 12:53:16 +04:00
|
|
|
}
|
2007-07-21 19:46:31 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getRelation
|
|
|
|
*
|
|
|
|
* @param string $alias relation alias
|
|
|
|
*/
|
|
|
|
public function getRelation($alias, $recursive = true)
|
|
|
|
{
|
|
|
|
return $this->_parser->getRelation($alias, $recursive);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getRelations
|
|
|
|
* returns an array containing all relation objects
|
|
|
|
*
|
|
|
|
* @return array an array of Doctrine_Relation objects
|
|
|
|
*/
|
|
|
|
public function getRelations()
|
|
|
|
{
|
|
|
|
return $this->_parser->getRelations();
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* createQuery
|
|
|
|
* creates a new Doctrine_Query object and adds the component name
|
|
|
|
* of this table as the query 'from' part
|
|
|
|
*
|
2007-10-02 15:28:26 +04:00
|
|
|
* @param string Optional alias name for component aliasing.
|
|
|
|
*
|
2007-05-28 00:00:53 +04:00
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
2007-10-02 15:28:26 +04:00
|
|
|
public function createQuery($alias = '')
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! empty($alias)) {
|
2007-10-02 15:28:26 +04:00
|
|
|
$alias = ' ' . trim($alias);
|
|
|
|
}
|
|
|
|
return Doctrine_Query::create($this->_conn)->from($this->getComponentName() . $alias);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getRepository
|
|
|
|
*
|
|
|
|
* @return Doctrine_Table_Repository
|
|
|
|
*/
|
|
|
|
public function getRepository()
|
|
|
|
{
|
2007-09-21 00:48:53 +04:00
|
|
|
return $this->_repository;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04: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;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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-10-21 10:23:59 +04:00
|
|
|
|
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
|
|
|
|
|
|
|
if (isset($options['primary'])) {
|
2007-11-18 19:06:37 +03:00
|
|
|
if (isset($this->_identifier)) {
|
|
|
|
$this->_identifier = (array) $this->_identifier;
|
|
|
|
}
|
|
|
|
if ( ! in_array($fieldName, $this->_identifier)) {
|
|
|
|
$this->_identifier[] = $fieldName;
|
|
|
|
}
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
|
|
|
if (isset($options['default'])) {
|
|
|
|
$this->hasDefaultValues = true;
|
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04: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;
|
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04: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
|
|
|
}
|
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
|
|
|
}
|
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-10-16 01:39:35 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* sets the connection for this class
|
|
|
|
*
|
|
|
|
* @params Doctrine_Connection a connection object
|
|
|
|
* @return Doctrine_Table this object
|
|
|
|
*/
|
|
|
|
public function setConnection(Doctrine_Connection $conn)
|
|
|
|
{
|
|
|
|
$this->_conn = $conn;
|
|
|
|
|
|
|
|
$this->setParent($this->_conn);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2007-10-16 01:39:35 +04:00
|
|
|
* returns the connection associated with this table (if any)
|
|
|
|
*
|
|
|
|
* @return Doctrine_Connection|null the connection object
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
|
|
|
public function getConnection()
|
|
|
|
{
|
2007-09-21 00:21:08 +04:00
|
|
|
return $this->_conn;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* creates a new record
|
|
|
|
*
|
2007-10-16 01:39:35 +04:00
|
|
|
* @param $array an array where keys are field names and
|
|
|
|
* values representing field values
|
|
|
|
* @return Doctrine_Record the created record object
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2007-10-16 01:39:35 +04:00
|
|
|
public function create(array $array = array())
|
|
|
|
{
|
2007-11-16 00:01:19 +03:00
|
|
|
$record = new $this->_options['name']($this, true);
|
|
|
|
$record->fromArray($array);
|
2007-10-16 01:39:35 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
return $record;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* finds a record by its identifier
|
|
|
|
*
|
|
|
|
* @param $id database row id
|
2007-09-06 18:12:40 +04:00
|
|
|
* @param int $hydrationMode Doctrine::HYDRATE_ARRAY or Doctrine::HYDRATE_RECORD
|
|
|
|
* @return mixed Array or Doctrine_Record or false if no result
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
2007-09-01 23:44:38 +04:00
|
|
|
public function find($id, $hydrationMode = null)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-09-06 18:12:40 +04:00
|
|
|
if (is_null($id)) {
|
|
|
|
return false;
|
2007-09-01 23:44:38 +04:00
|
|
|
}
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-09-06 18:12:40 +04:00
|
|
|
$id = is_array($id) ? array_values($id) : array($id);
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-09-06 18:12:40 +04:00
|
|
|
return $this->createQuery()
|
2007-11-18 19:06:37 +03:00
|
|
|
->where(implode(' = ? AND ', $this->getIdentifierColumnNames()) . ' = ?')
|
2007-09-06 18:12:40 +04:00
|
|
|
->fetchOne($id, $hydrationMode);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* findAll
|
|
|
|
* returns a collection of records
|
|
|
|
*
|
2007-09-01 23:44:38 +04:00
|
|
|
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
|
2007-05-28 00:00:53 +04:00
|
|
|
* @return Doctrine_Collection
|
|
|
|
*/
|
2007-09-01 23:44:38 +04:00
|
|
|
public function findAll($hydrationMode = null)
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-10-02 15:28:26 +04:00
|
|
|
return $this->createQuery()->execute(array(), $hydrationMode);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* findByDql
|
|
|
|
* finds records with given DQL where clause
|
|
|
|
* returns a collection of records
|
|
|
|
*
|
|
|
|
* @param string $dql DQL after WHERE clause
|
|
|
|
* @param array $params query parameters
|
2007-09-01 23:44:38 +04:00
|
|
|
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
|
2007-05-28 00:00:53 +04:00
|
|
|
* @return Doctrine_Collection
|
|
|
|
*/
|
2007-10-02 15:28:26 +04:00
|
|
|
public function findBySql($dql, array $params = array(), $hydrationMode = null)
|
|
|
|
{
|
|
|
|
return $this->createQuery()->where($dql)->execute($params, $hydrationMode);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
|
|
|
|
2007-10-02 15:28:26 +04:00
|
|
|
public function findByDql($dql, array $params = array(), $hydrationMode = null)
|
|
|
|
{
|
2007-09-01 23:44:38 +04:00
|
|
|
return $this->findBySql($dql, $params, $hydrationMode);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-08 20:39:12 +04:00
|
|
|
/**
|
|
|
|
* execute
|
|
|
|
* fetches data using the provided queryKey and
|
|
|
|
* the associated query in the query registry
|
|
|
|
*
|
|
|
|
* if no query for given queryKey is being found a
|
|
|
|
* Doctrine_Query_Registry exception is being thrown
|
|
|
|
*
|
|
|
|
* @param string $queryKey the query key
|
|
|
|
* @param array $params prepared statement params (if any)
|
|
|
|
* @return mixed the fetched data
|
|
|
|
*/
|
|
|
|
public function execute($queryKey, $params = array(), $hydrationMode = Doctrine::HYDRATE_RECORD)
|
|
|
|
{
|
|
|
|
return Doctrine_Manager::getInstance()
|
|
|
|
->getQueryRegistry()
|
|
|
|
->get($queryKey, $this->getComponentName())
|
|
|
|
->execute($params, $hydrationMode);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-08 20:39:12 +04:00
|
|
|
/**
|
|
|
|
* executeOne
|
|
|
|
* fetches data using the provided queryKey and
|
|
|
|
* the associated query in the query registry
|
|
|
|
*
|
|
|
|
* if no query for given queryKey is being found a
|
|
|
|
* Doctrine_Query_Registry exception is being thrown
|
|
|
|
*
|
|
|
|
* @param string $queryKey the query key
|
|
|
|
* @param array $params prepared statement params (if any)
|
|
|
|
* @return mixed the fetched data
|
|
|
|
*/
|
|
|
|
public function executeOne($queryKey, $params = array(), $hydrationMode = Doctrine::HYDRATE_RECORD)
|
|
|
|
{
|
|
|
|
return Doctrine_Manager::getInstance()
|
|
|
|
->getQueryRegistry()
|
|
|
|
->get($queryKey, $this->getComponentName())
|
|
|
|
->fetchOne($params, $hydrationMode);
|
|
|
|
}
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* clear
|
|
|
|
* clears the first level cache (identityMap)
|
|
|
|
*
|
|
|
|
* @return void
|
2007-11-18 19:06:37 +03:00
|
|
|
* @todo what about a more descriptive name? clearIdentityMap?
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
2007-09-21 00:48:53 +04:00
|
|
|
$this->_identityMap = array();
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-06 03:21:29 +04:00
|
|
|
/**
|
|
|
|
* addRecord
|
|
|
|
* adds a record to identity map
|
|
|
|
*
|
|
|
|
* @param Doctrine_Record $record record to be added
|
|
|
|
* @return boolean
|
2007-11-19 13:00:44 +03:00
|
|
|
* @todo Better name? registerRecord?
|
2007-07-06 03:21:29 +04:00
|
|
|
*/
|
|
|
|
public function addRecord(Doctrine_Record $record)
|
|
|
|
{
|
|
|
|
$id = implode(' ', $record->identifier());
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-09-21 00:48:53 +04:00
|
|
|
if (isset($this->_identityMap[$id])) {
|
2007-07-06 03:21:29 +04:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-09-21 00:48:53 +04:00
|
|
|
$this->_identityMap[$id] = $record;
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-07-06 03:21:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-11-09 01:35:55 +03:00
|
|
|
/**
|
|
|
|
* removeRecord
|
|
|
|
* removes a record from the identity map, returning true if the record
|
|
|
|
* was found and removed and false if the record wasn't found.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Record $record record to be removed
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function removeRecord(Doctrine_Record $record)
|
|
|
|
{
|
|
|
|
$id = implode(' ', $record->identifier());
|
|
|
|
|
|
|
|
if (isset($this->_identityMap[$id])) {
|
|
|
|
unset($this->_identityMap[$id]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* getRecord
|
|
|
|
* first checks if record exists in identityMap, if not
|
|
|
|
* returns a new record
|
|
|
|
*
|
|
|
|
* @return Doctrine_Record
|
|
|
|
*/
|
|
|
|
public function getRecord()
|
|
|
|
{
|
2007-09-21 00:53:15 +04:00
|
|
|
if ( ! empty($this->_data)) {
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
$identifierFieldNames = $this->getIdentifier();
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
if ( ! is_array($identifierFieldNames)) {
|
|
|
|
$identifierFieldNames = array($identifierFieldNames);
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-05-31 21:45:07 +04:00
|
|
|
$found = false;
|
2007-11-18 19:06:37 +03:00
|
|
|
foreach ($identifierFieldNames as $fieldName) {
|
|
|
|
if ( ! isset($this->_data[$fieldName])) {
|
2007-05-31 21:45:07 +04:00
|
|
|
// primary key column not found return new record
|
|
|
|
$found = true;
|
|
|
|
break;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
$id[] = $this->_data[$fieldName];
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-05-31 21:45:07 +04:00
|
|
|
if ($found) {
|
|
|
|
$recordName = $this->getClassnameToReturn();
|
2007-07-16 23:19:29 +04:00
|
|
|
$record = new $recordName($this, true);
|
2007-09-21 00:53:15 +04:00
|
|
|
$this->_data = array();
|
2007-05-31 21:45:07 +04:00
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
$id = implode(' ', $id);
|
2007-09-02 12:53:16 +04:00
|
|
|
|
2007-09-21 00:48:53 +04:00
|
|
|
if (isset($this->_identityMap[$id])) {
|
|
|
|
$record = $this->_identityMap[$id];
|
2007-09-21 00:53:15 +04:00
|
|
|
$record->hydrate($this->_data);
|
2007-05-28 00:00:53 +04:00
|
|
|
} else {
|
|
|
|
$recordName = $this->getClassnameToReturn();
|
|
|
|
$record = new $recordName($this);
|
2007-09-21 00:48:53 +04:00
|
|
|
$this->_identityMap[$id] = $record;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-09-21 00:53:15 +04:00
|
|
|
$this->_data = array();
|
2007-05-28 00:00:53 +04:00
|
|
|
} else {
|
|
|
|
$recordName = $this->getClassnameToReturn();
|
|
|
|
$record = new $recordName($this, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the classname to return. Most often this is just the options['name']
|
|
|
|
*
|
2007-09-02 12:53:16 +04:00
|
|
|
* Check the subclasses option and the inheritanceMap for each subclass to see
|
|
|
|
* if all the maps in a subclass is met. If this is the case return that
|
|
|
|
* subclass name. If no subclasses match or if there are no subclasses defined
|
2007-05-28 00:00:53 +04:00
|
|
|
* return the name of the class for this tables record.
|
|
|
|
*
|
2007-09-02 12:53:16 +04:00
|
|
|
* @todo this function could use reflection to check the first time it runs
|
|
|
|
* if the subclassing option is not set.
|
2007-05-28 00:00:53 +04:00
|
|
|
*
|
|
|
|
* @return string The name of the class to create
|
|
|
|
*
|
2007-09-02 12:53:16 +04:00
|
|
|
*/
|
2007-05-28 00:00:53 +04:00
|
|
|
public function getClassnameToReturn()
|
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
if ( ! isset($this->_options['subclasses'])) {
|
|
|
|
return $this->_options['name'];
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-06 01:18:40 +04:00
|
|
|
foreach ($this->_options['subclasses'] as $subclass) {
|
2007-09-21 00:21:08 +04:00
|
|
|
$table = $this->_conn->getTable($subclass);
|
2007-05-28 00:00:53 +04:00
|
|
|
$inheritanceMap = $table->getOption('inheritanceMap');
|
|
|
|
$nomatch = false;
|
|
|
|
foreach ($inheritanceMap as $key => $value) {
|
2007-09-21 00:53:15 +04:00
|
|
|
if ( ! isset($this->_data[$key]) || $this->_data[$key] != $value) {
|
2007-05-28 00:00:53 +04:00
|
|
|
$nomatch = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( ! $nomatch) {
|
|
|
|
return $table->getComponentName();
|
|
|
|
}
|
|
|
|
}
|
2007-10-06 01:18:40 +04:00
|
|
|
return $this->_options['name'];
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $id database row id
|
|
|
|
* @throws Doctrine_Find_Exception
|
|
|
|
*/
|
|
|
|
final public function getProxy($id = null)
|
|
|
|
{
|
|
|
|
if ($id !== null) {
|
2007-11-18 19:06:37 +03:00
|
|
|
$identifierColumnNames = $this->getIdentifierColumnNames();
|
|
|
|
$query = 'SELECT ' . implode(', ', (array) $identifierColumnNames)
|
2007-09-02 12:53:16 +04:00
|
|
|
. ' FROM ' . $this->getTableName()
|
2007-11-18 19:06:37 +03:00
|
|
|
. ' WHERE ' . implode(' = ? && ', (array) $identifierColumnNames) . ' = ?';
|
2007-05-28 00:00:53 +04:00
|
|
|
$query = $this->applyInheritance($query);
|
|
|
|
|
2007-10-06 01:18:40 +04:00
|
|
|
$params = array_merge(array($id), array_values($this->_options['inheritanceMap']));
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-09-21 00:53:15 +04:00
|
|
|
$this->_data = $this->_conn->execute($query, $params)->fetch(PDO::FETCH_ASSOC);
|
2007-05-28 00:00:53 +04:00
|
|
|
|
2007-09-21 00:53:15 +04:00
|
|
|
if ($this->_data === false)
|
2007-05-28 00:00:53 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->getRecord();
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-02 15:28:26 +04:00
|
|
|
/**
|
|
|
|
* applyInheritance
|
|
|
|
* @param $where query where part to be modified
|
|
|
|
* @return string query where part with column aggregation inheritance added
|
|
|
|
*/
|
|
|
|
final public function applyInheritance($where)
|
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
if ( ! empty($this->_options['inheritanceMap'])) {
|
2007-10-02 15:28:26 +04:00
|
|
|
$a = array();
|
2007-10-06 01:18:40 +04:00
|
|
|
foreach ($this->_options['inheritanceMap'] as $field => $value) {
|
2007-11-18 19:06:37 +03:00
|
|
|
$a[] = $this->getColumnName($field) . ' = ?';
|
2007-10-02 15:28:26 +04:00
|
|
|
}
|
|
|
|
$i = implode(' AND ', $a);
|
|
|
|
$where .= ' AND ' . $i;
|
|
|
|
}
|
|
|
|
return $where;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* count
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
$a = $this->_conn->execute('SELECT COUNT(1) FROM ' . $this->_options['tableName'])->fetch(Doctrine::FETCH_NUM);
|
2007-05-28 00:00:53 +04:00
|
|
|
return current($a);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2007-11-18 19:06:37 +03:00
|
|
|
* @return Doctrine_Query a Doctrine_Query object
|
2007-05-28 00:00:53 +04:00
|
|
|
*/
|
|
|
|
public function getQueryObject()
|
|
|
|
{
|
|
|
|
$graph = new Doctrine_Query($this->getConnection());
|
|
|
|
$graph->load($this->getComponentName());
|
|
|
|
return $graph;
|
|
|
|
}
|
2007-10-21 10:23:59 +04: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);
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! $this->_conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)
|
2007-11-18 19:06:37 +03:00
|
|
|
&& isset($this->_columns[$columnName]['values'][$index])
|
2007-09-02 12:53:16 +04:00
|
|
|
) {
|
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);
|
2007-09-21 00:21:08 +04: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-18 19:06:37 +03:00
|
|
|
public function removeColumn($columnName)
|
2007-10-06 01:18:40 +04:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-18 19:06:37 +03:00
|
|
|
$columnName = $this->getColumnName($fieldName);
|
|
|
|
if (isset($this->_columns[$columnName])) {
|
|
|
|
return $this->_columns[$columnName]['type'];
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* setData
|
|
|
|
* doctrine uses this function internally
|
|
|
|
* users are strongly discouraged to use this function
|
|
|
|
*
|
|
|
|
* @param array $data internal data
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setData(array $data)
|
|
|
|
{
|
2007-09-21 00:53:15 +04:00
|
|
|
$this->_data = $data;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* returns internal data, used by Doctrine_Record instances
|
|
|
|
* when retrieving data from database
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2007-06-28 15:56:56 +04:00
|
|
|
public function getData()
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-09-21 00:53:15 +04:00
|
|
|
return $this->_data;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-05 23:04:37 +04:00
|
|
|
/**
|
|
|
|
* prepareValue
|
2007-09-02 12:53:16 +04:00
|
|
|
* this method performs special data preparation depending on
|
2007-07-05 23:04:37 +04:00
|
|
|
* the type of the given column
|
|
|
|
*
|
|
|
|
* 1. It unserializes array and object typed columns
|
|
|
|
* 2. Uncompresses gzip typed columns
|
|
|
|
* 3. Gets the appropriate enum values for enum typed columns
|
|
|
|
* 4. Initializes special null object pointer for null values (for fast column existence checking purposes)
|
|
|
|
*
|
|
|
|
* example:
|
|
|
|
* <code type='php'>
|
|
|
|
* $field = 'name';
|
|
|
|
* $value = null;
|
|
|
|
* $table->prepareValue($field, $value); // Doctrine_Null
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Table_Exception if unserialization of array/object typed column fails or
|
|
|
|
* @throws Doctrine_Table_Exception if uncompression of gzip typed column fails *
|
|
|
|
* @param string $field the name of the field
|
|
|
|
* @param string $value field value
|
|
|
|
* @return mixed prepared value
|
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
public function prepareValue($fieldName, $value)
|
2007-07-05 23:04:37 +04:00
|
|
|
{
|
2007-07-21 20:49:52 +04:00
|
|
|
if ($value === self::$_null) {
|
2007-07-05 23:04:37 +04:00
|
|
|
return self::$_null;
|
2007-10-22 21:33:47 +04:00
|
|
|
} elseif ($value === null) {
|
2007-07-21 20:49:52 +04:00
|
|
|
return null;
|
2007-07-05 23:04:37 +04:00
|
|
|
} else {
|
2007-11-18 19:06:37 +03:00
|
|
|
$type = $this->getTypeOf($fieldName);
|
2007-07-05 23:04:37 +04:00
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case 'array':
|
|
|
|
case 'object':
|
|
|
|
if (is_string($value)) {
|
|
|
|
$value = unserialize($value);
|
|
|
|
|
|
|
|
if ($value === false) {
|
2007-11-18 19:06:37 +03:00
|
|
|
throw new Doctrine_Table_Exception('Unserialization of ' . $fieldName . ' failed.');
|
2007-07-05 23:04:37 +04:00
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'gzip':
|
|
|
|
$value = gzuncompress($value);
|
|
|
|
|
|
|
|
if ($value === false) {
|
2007-11-18 19:06:37 +03:00
|
|
|
throw new Doctrine_Table_Exception('Uncompressing of ' . $fieldName . ' failed.');
|
2007-07-05 23:04:37 +04:00
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
break;
|
|
|
|
case 'enum':
|
2007-11-18 19:06:37 +03:00
|
|
|
return $this->enumValue($fieldName, $value);
|
2007-07-05 23:04:37 +04:00
|
|
|
break;
|
|
|
|
case 'boolean':
|
|
|
|
return (boolean) $value;
|
|
|
|
break;
|
|
|
|
case 'integer':
|
2007-07-20 13:38:10 +04:00
|
|
|
// don't do any casting here PHP INT_MAX is smaller than what the databases support
|
2007-07-06 03:21:29 +04:00
|
|
|
break;
|
2007-07-05 23:04:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2007-10-21 11:24:18 +04:00
|
|
|
* getTree
|
|
|
|
*
|
2007-05-28 00:00:53 +04:00
|
|
|
* getter for associated tree
|
|
|
|
*
|
|
|
|
* @return mixed if tree return instance of Doctrine_Tree, otherwise returns false
|
2007-09-02 12:53:16 +04:00
|
|
|
*/
|
2007-10-21 11:24:18 +04:00
|
|
|
public function getTree()
|
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
if (isset($this->_options['treeImpl'])) {
|
2007-09-21 00:53:15 +04:00
|
|
|
if ( ! $this->_tree) {
|
2007-10-06 01:18:40 +04:00
|
|
|
$options = isset($this->_options['treeOptions']) ? $this->_options['treeOptions'] : array();
|
2007-09-21 00:53:15 +04:00
|
|
|
$this->_tree = Doctrine_Tree::factory($this,
|
2007-10-06 01:18:40 +04:00
|
|
|
$this->_options['treeImpl'],
|
2007-05-28 00:00:53 +04:00
|
|
|
$options
|
|
|
|
);
|
|
|
|
}
|
2007-09-21 00:53:15 +04:00
|
|
|
return $this->_tree;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2007-10-21 11:24:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getComponentName
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-09-02 12:53:16 +04:00
|
|
|
public function getComponentName()
|
2007-05-28 00:00:53 +04:00
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
return $this->_options['name'];
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 11:24:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getTableName
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-05-28 00:00:53 +04:00
|
|
|
public function getTableName()
|
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
return $this->_options['tableName'];
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 11:24:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* setTableName
|
|
|
|
*
|
|
|
|
* @param string $tableName
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-05-28 00:00:53 +04:00
|
|
|
public function setTableName($tableName)
|
|
|
|
{
|
2007-10-30 16:30:50 +03:00
|
|
|
$this->setOption('tableName', $this->_conn->formatter->getTableName($tableName));
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
2007-10-21 11:24:18 +04:00
|
|
|
* isTree
|
|
|
|
*
|
2007-05-28 00:00:53 +04:00
|
|
|
* determine if table acts as tree
|
|
|
|
*
|
|
|
|
* @return mixed if tree return true, otherwise returns false
|
2007-09-02 12:53:16 +04:00
|
|
|
*/
|
2007-10-21 11:24:18 +04:00
|
|
|
public function isTree()
|
|
|
|
{
|
2007-10-06 01:18:40 +04:00
|
|
|
return ( ! is_null($this->_options['treeImpl'])) ? true : false;
|
2007-05-28 00:00:53 +04:00
|
|
|
}
|
2007-07-18 00:59:09 +04:00
|
|
|
|
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;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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-21 11:24:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* unshiftFilter
|
|
|
|
*
|
|
|
|
* @param object Doctrine_Record_Filter $filter
|
|
|
|
* @return object $this
|
|
|
|
*/
|
2007-09-21 17:48:31 +04:00
|
|
|
public function unshiftFilter(Doctrine_Record_Filter $filter)
|
|
|
|
{
|
2007-10-02 15:28:26 +04:00
|
|
|
$filter->setTable($this);
|
|
|
|
|
|
|
|
$filter->init();
|
2007-09-21 17:48:31 +04:00
|
|
|
|
|
|
|
array_unshift($this->_filters, $filter);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 11:24:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getFilters
|
|
|
|
*
|
|
|
|
* @return array $filters
|
|
|
|
*/
|
2007-09-21 17:48:31 +04:00
|
|
|
public function getFilters()
|
|
|
|
{
|
|
|
|
return $this->_filters;
|
2007-06-08 23:27:38 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-28 00:00:53 +04:00
|
|
|
/**
|
|
|
|
* returns a string representation of this object
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return Doctrine_Lib::getTableAsString($this);
|
|
|
|
}
|
2007-10-21 11:24:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* findBy
|
|
|
|
*
|
|
|
|
* @param string $column
|
|
|
|
* @param string $value
|
|
|
|
* @param string $hydrationMode
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
protected function findBy($fieldName, $value, $hydrationMode = null)
|
2007-10-21 11:24:18 +04:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
return $this->createQuery()->where($fieldName . ' = ?')->execute(array($value), $hydrationMode);
|
2007-10-21 11:24:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* findOneBy
|
|
|
|
*
|
|
|
|
* @param string $column
|
|
|
|
* @param string $value
|
|
|
|
* @param string $hydrationMode
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-11-18 19:06:37 +03:00
|
|
|
protected function findOneBy($fieldName, $value, $hydrationMode = null)
|
2007-10-21 11:24:18 +04:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
$results = $this->createQuery()->where($fieldName . ' = ?')->limit(1)->execute(array($value), $hydrationMode);
|
2007-10-21 11:24:18 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
return $hydrationMode === Doctrine::FETCH_ARRAY ? $results[0] : $results->getFirst();
|
2007-10-21 11:24:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __call
|
|
|
|
*
|
|
|
|
* Adds support for magic finders.
|
|
|
|
* findByColumnName, findByRelationAlias
|
|
|
|
* findById, findByContactId, etc.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __call($method, $arguments)
|
|
|
|
{
|
|
|
|
if (substr($method, 0, 6) == 'findBy') {
|
|
|
|
$by = substr($method, 6, strlen($method));
|
|
|
|
$method = 'findBy';
|
|
|
|
} else if (substr($method, 0, 9) == 'findOneBy') {
|
|
|
|
$by = substr($method, 9, strlen($method));
|
|
|
|
$method = 'findOneBy';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($by)) {
|
2007-11-18 19:06:37 +03:00
|
|
|
if ( ! isset($arguments[0])) {
|
2007-10-21 11:24:18 +04:00
|
|
|
throw new Doctrine_Table_Exception('You must specify the value to findBy');
|
|
|
|
}
|
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
$fieldName = Doctrine::tableize($by);
|
2007-10-21 11:24:18 +04:00
|
|
|
$hydrationMode = isset($arguments[1]) ? $arguments[1]:null;
|
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
if ($this->hasColumn($fieldName)) {
|
|
|
|
return $this->$method($fieldName, $arguments[0], $hydrationMode);
|
2007-10-21 11:24:18 +04:00
|
|
|
} else if ($this->hasRelation($by)) {
|
|
|
|
$relation = $this->getRelation($by);
|
|
|
|
|
|
|
|
if ($relation['type'] === Doctrine_Relation::MANY) {
|
|
|
|
throw new Doctrine_Table_Exception('Cannot findBy many relationship.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->$method($relation['local'], $arguments[0], $hydrationMode);
|
|
|
|
} else {
|
|
|
|
throw new Doctrine_Table_Exception('Cannot find by: ' . $by . '. Invalid column or relationship alias.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-22 21:33:47 +04:00
|
|
|
}
|