2006-05-30 12:42:10 +04:00
|
|
|
<?php
|
2006-08-31 14:00:09 +04:00
|
|
|
/*
|
|
|
|
* $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>.
|
|
|
|
*/
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* Doctrine_Table represents a database table
|
|
|
|
* each Doctrine_Table holds the information of foreignKeys and associations
|
2006-07-26 21:17:59 +04:00
|
|
|
*
|
2006-05-30 12:42:10 +04:00
|
|
|
*
|
2006-12-01 01:33:54 +03:00
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @package Doctrine
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @version $Revision$
|
|
|
|
* @category Object Relational Mapping
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
class Doctrine_Table extends Doctrine_Configurable implements Countable
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* @var array $data temporary data which is then loaded into Doctrine_Record::$data
|
|
|
|
*/
|
|
|
|
private $data = array();
|
|
|
|
/**
|
|
|
|
* @var array $relations an array containing all the Doctrine_Relation objects for this table
|
|
|
|
*/
|
|
|
|
private $relations = array();
|
|
|
|
/**
|
|
|
|
* @var array $primaryKeys an array containing all primary key column names
|
|
|
|
*/
|
|
|
|
private $primaryKeys = array();
|
|
|
|
/**
|
|
|
|
* @var mixed $identifier
|
|
|
|
*/
|
|
|
|
private $identifier;
|
|
|
|
/**
|
2006-09-09 03:20:51 +04:00
|
|
|
* @see Doctrine_Identifier constants
|
|
|
|
* @var integer $identifierType the type of identifier this table uses
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
|
|
|
private $identifierType;
|
|
|
|
/**
|
|
|
|
* @var string $query cached simple query
|
|
|
|
*/
|
|
|
|
private $query;
|
|
|
|
/**
|
2006-12-01 01:33:54 +03:00
|
|
|
* @var Doctrine_Connection $conn Doctrine_Connection object that created this table
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
2006-12-01 01:33:54 +03:00
|
|
|
private $conn;
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
2006-10-24 21:02:47 +04:00
|
|
|
* @var string $name
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
|
|
|
private $name;
|
|
|
|
/**
|
|
|
|
* @var array $identityMap first level cache
|
|
|
|
*/
|
|
|
|
private $identityMap = array();
|
2006-06-01 15:58:05 +04:00
|
|
|
/**
|
2006-09-30 13:54:12 +04:00
|
|
|
* @var Doctrine_Table_Repository $repository record repository
|
2006-06-01 15:58:05 +04:00
|
|
|
*/
|
2006-05-30 12:42:10 +04:00
|
|
|
private $repository;
|
|
|
|
/**
|
2006-10-24 21:02:47 +04:00
|
|
|
* @var array $columns an array of column definitions,
|
|
|
|
* keys as column names and values as column definitions
|
2006-12-29 17:01:31 +03:00
|
|
|
*
|
2006-10-24 21:02:47 +04:00
|
|
|
* the value array has three values:
|
2006-12-29 17:01:31 +03:00
|
|
|
*
|
2006-10-24 21:02:47 +04:00
|
|
|
* the column type, eg. 'integer'
|
|
|
|
* the column length, eg. 11
|
|
|
|
* the column options/constraints/validators. eg array('notnull' => true)
|
|
|
|
*
|
|
|
|
* so the full columns array might look something like the following:
|
|
|
|
* array(
|
2006-10-27 00:53:59 +04:00
|
|
|
* 'name' => array('string', 20, array('notnull' => true, 'default' => 'someone')),
|
|
|
|
* 'age' => array('integer', 11, array('notnull' => true))
|
2006-10-24 21:02:47 +04:00
|
|
|
* )
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
2006-10-24 21:02:47 +04:00
|
|
|
protected $columns = array();
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
2007-01-23 19:27:20 +03:00
|
|
|
* @var array $columnAliases an array of column aliases
|
|
|
|
* keys as column aliases and values as column names
|
|
|
|
*/
|
|
|
|
protected $columnAliases = array();
|
|
|
|
/**
|
|
|
|
* @var array $bound bound relations
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
|
|
|
private $bound = array();
|
|
|
|
/**
|
2007-01-23 19:27:20 +03:00
|
|
|
* @var array $boundAliases bound relation aliases
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
|
|
|
private $boundAliases = array();
|
|
|
|
/**
|
2007-01-23 19:27:20 +03:00
|
|
|
* @var integer $columnCount cached column count, Doctrine_Record uses this column count in when
|
|
|
|
* determining its state
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
|
|
|
private $columnCount;
|
2006-09-16 00:56:25 +04:00
|
|
|
/**
|
2007-01-23 19:27:20 +03:00
|
|
|
* @var boolean $hasDefaultValues whether or not this table has default values
|
2006-09-16 00:56:25 +04:00
|
|
|
*/
|
|
|
|
private $hasDefaultValues;
|
2006-10-24 21:02:47 +04:00
|
|
|
/**
|
2006-10-27 00:53:59 +04:00
|
|
|
* @var array $options an array containing all options
|
2006-10-24 21:02:47 +04:00
|
|
|
*
|
2006-10-27 00:53:59 +04:00
|
|
|
* -- name name of the component, for example component name of the GroupTable is 'Group'
|
2006-10-24 21:02:47 +04:00
|
|
|
*
|
2007-02-08 16:56:23 +03:00
|
|
|
* -- parents the parent classes of this component
|
|
|
|
*
|
2007-01-12 01:39:32 +03:00
|
|
|
* -- declaringClass name of the table definition declaring class (when using inheritance the class
|
|
|
|
* that defines the table structure can be any class in the inheritance hierarchy,
|
|
|
|
* hence we need reflection to check out which class actually calls setTableDefinition)
|
|
|
|
*
|
2006-10-27 00:53:59 +04:00
|
|
|
* -- 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
|
2006-10-24 21:02:47 +04:00
|
|
|
*
|
2006-10-27 00:53:59 +04:00
|
|
|
* -- 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
|
2006-10-24 21:02:47 +04:00
|
|
|
*
|
2006-10-27 00:53:59 +04:00
|
|
|
* -- enumMap enum value arrays
|
2006-10-24 21:02:47 +04:00
|
|
|
*
|
2006-10-27 00:53:59 +04:00
|
|
|
* -- inheritanceMap inheritanceMap is used for inheritance mapping, keys representing columns and values
|
|
|
|
* the column values that should correspond to child classes
|
2007-01-16 22:27:33 +03:00
|
|
|
*
|
2007-02-12 01:53:35 +03:00
|
|
|
* -- type table type (mysql example: INNODB)
|
2007-01-16 22:27:33 +03:00
|
|
|
*
|
|
|
|
* -- charset character set
|
|
|
|
*
|
2007-02-17 01:54:59 +03:00
|
|
|
* -- foreignKeys the foreign keys of this table
|
|
|
|
*
|
2007-01-16 22:27:33 +03:00
|
|
|
* -- collation
|
2007-01-25 14:37:46 +03:00
|
|
|
*
|
2007-02-12 01:53:35 +03:00
|
|
|
* -- indexes the index definitions of this table
|
2007-02-08 01:54:03 +03:00
|
|
|
*
|
|
|
|
* -- treeImpl the tree implementation of this table (if any)
|
|
|
|
*
|
|
|
|
* -- treeOptions the tree options
|
2006-10-24 21:02:47 +04:00
|
|
|
*/
|
|
|
|
protected $options = array('name' => null,
|
2007-04-11 23:06:13 +04:00
|
|
|
'tableName' => null,
|
|
|
|
'sequenceName' => null,
|
|
|
|
'inheritanceMap' => array(),
|
|
|
|
'enumMap' => array(),
|
|
|
|
'engine' => null,
|
|
|
|
'charset' => null,
|
|
|
|
'collation' => null,
|
|
|
|
'treeImpl' => null,
|
|
|
|
'treeOptions' => null,
|
|
|
|
'indexes' => array(),
|
|
|
|
);
|
2007-02-08 02:10:41 +03:00
|
|
|
/**
|
|
|
|
* @var Doctrine_Tree $tree tree object associated with this table
|
|
|
|
*/
|
|
|
|
protected $tree;
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* the constructor
|
2006-09-18 23:52:17 +04:00
|
|
|
* @throws Doctrine_Connection_Exception if there are no opened connections
|
|
|
|
* @throws Doctrine_Table_Exception if there is already an instance of this table
|
2006-05-30 12:42:10 +04:00
|
|
|
* @return void
|
|
|
|
*/
|
2007-04-03 21:36:46 +04:00
|
|
|
public function __construct($name, Doctrine_Connection $conn)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2006-12-01 01:33:54 +03:00
|
|
|
$this->conn = $conn;
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2006-12-01 01:33:54 +03:00
|
|
|
$this->setParent($this->conn);
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2006-10-24 21:02:47 +04:00
|
|
|
$this->options['name'] = $name;
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! class_exists($name) || empty($name)) {
|
2006-05-30 12:42:10 +04:00
|
|
|
throw new Doctrine_Exception("Couldn't find class $name");
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
$record = new $name($this);
|
|
|
|
|
|
|
|
$names = array();
|
|
|
|
|
|
|
|
$class = $name;
|
|
|
|
|
|
|
|
// get parent classes
|
|
|
|
|
|
|
|
do {
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($class == "Doctrine_Record")
|
2006-09-18 23:52:17 +04:00
|
|
|
break;
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
$name = $class;
|
2006-05-30 12:42:10 +04:00
|
|
|
$names[] = $name;
|
2006-12-29 17:01:31 +03:00
|
|
|
} while ($class = get_parent_class($class));
|
2006-05-30 12:42:10 +04:00
|
|
|
|
|
|
|
// reverse names
|
|
|
|
$names = array_reverse($names);
|
|
|
|
|
|
|
|
// create database table
|
2006-12-29 17:01:31 +03:00
|
|
|
if (method_exists($record, 'setTableDefinition')) {
|
2006-05-30 12:42:10 +04:00
|
|
|
$record->setTableDefinition();
|
|
|
|
|
2007-02-08 15:53:32 +03:00
|
|
|
// set the table definition for the given tree implementation
|
|
|
|
if($this->isTree())
|
|
|
|
$this->getTree()->setTableDefinition();
|
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
$this->columnCount = count($this->columns);
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($this->columns)) {
|
2006-09-18 23:52:17 +04:00
|
|
|
// get the declaring class of setTableDefinition method
|
2006-10-26 00:02:40 +04:00
|
|
|
$method = new ReflectionMethod($this->options['name'], 'setTableDefinition');
|
2006-05-30 12:42:10 +04:00
|
|
|
$class = $method->getDeclaringClass();
|
|
|
|
|
2007-01-12 01:39:32 +03:00
|
|
|
$this->options['declaringClass'] = $class;
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! isset($this->options['tableName'])) {
|
2006-10-24 21:02:47 +04:00
|
|
|
$this->options['tableName'] = Doctrine::tableize($class->getName());
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
switch (count($this->primaryKeys)) {
|
2007-04-11 23:06:13 +04:00
|
|
|
case 0:
|
|
|
|
$this->columns = array_merge(array('id' =>
|
|
|
|
array('integer',
|
|
|
|
20,
|
|
|
|
array('autoincrement' => true,
|
|
|
|
'primary' => true,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
), $this->columns);
|
|
|
|
|
|
|
|
$this->primaryKeys[] = 'id';
|
|
|
|
$this->identifier = 'id';
|
|
|
|
$this->identifierType = Doctrine_Identifier::AUTO_INCREMENT;
|
|
|
|
$this->columnCount++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (count($this->primaryKeys) > 1) {
|
|
|
|
$this->identifier = $this->primaryKeys;
|
|
|
|
$this->identifierType = Doctrine_Identifier::COMPOSITE;
|
2006-12-30 00:46:14 +03:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
} else {
|
|
|
|
foreach ($this->primaryKeys as $pk) {
|
|
|
|
$e = $this->columns[$pk][2];
|
|
|
|
|
|
|
|
$found = false;
|
|
|
|
|
|
|
|
foreach ($e as $option => $value) {
|
|
|
|
if ($found)
|
|
|
|
break;
|
|
|
|
|
|
|
|
$e2 = explode(':', $option);
|
|
|
|
|
|
|
|
switch (strtolower($e2[0])) {
|
|
|
|
case 'autoincrement':
|
|
|
|
case 'autoinc':
|
|
|
|
$this->identifierType = Doctrine_Identifier::AUTO_INCREMENT;
|
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
case 'seq':
|
|
|
|
case 'sequence':
|
|
|
|
$this->identifierType = Doctrine_Identifier::SEQUENCE;
|
|
|
|
$found = true;
|
|
|
|
|
|
|
|
if ($value) {
|
|
|
|
$this->options['sequenceName'] = $value;
|
|
|
|
} else {
|
|
|
|
if (($sequence = $this->getAttribute(Doctrine::ATTR_DEFAULT_SEQUENCE)) !== null) {
|
|
|
|
$this->options['sequenceName'] = $sequence;
|
|
|
|
} else {
|
|
|
|
$this->options['sequenceName'] = $this->conn->getSequenceName($this->options['tableName']);
|
|
|
|
}
|
2007-01-10 23:36:29 +03:00
|
|
|
}
|
2007-04-11 23:06:13 +04:00
|
|
|
break;
|
2006-12-30 00:30:37 +03:00
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2007-04-11 23:06:13 +04:00
|
|
|
if ( ! isset($this->identifierType)) {
|
|
|
|
$this->identifierType = Doctrine_Identifier::NORMAL;
|
|
|
|
}
|
|
|
|
$this->identifier = $pk;
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2007-04-11 23:06:13 +04:00
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
};
|
2007-01-21 21:31:51 +03:00
|
|
|
/**
|
|
|
|
if ( ! isset($definition['values'])) {
|
|
|
|
throw new Doctrine_Table_Exception('No values set for enum column ' . $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_array($definition['values'])) {
|
|
|
|
throw new Doctrine_Table_Exception('Enum column values should be specified as an array.');
|
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
*/
|
2007-03-22 01:11:18 +03:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
2006-10-20 22:21:42 +04:00
|
|
|
throw new Doctrine_Table_Exception("Class '$name' has no table definition.");
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2006-07-26 21:17:59 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
$record->setUp();
|
|
|
|
|
2007-02-08 15:53:32 +03:00
|
|
|
// if tree, set up tree
|
2007-02-17 15:38:02 +03:00
|
|
|
if ($this->isTree()) {
|
2007-02-08 15:53:32 +03:00
|
|
|
$this->getTree()->setUp();
|
2007-02-17 15:38:02 +03:00
|
|
|
}
|
2007-02-08 15:53:32 +03:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
// save parents
|
|
|
|
array_pop($names);
|
2007-02-08 16:56:23 +03:00
|
|
|
$this->options['parents'] = $names;
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2007-01-21 21:31:51 +03:00
|
|
|
$this->query = 'SELECT ' . implode(', ', array_keys($this->columns)) . ' FROM ' . $this->getTableName();
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2007-02-17 15:38:02 +03:00
|
|
|
|
2006-09-30 13:54:12 +04:00
|
|
|
$this->repository = new Doctrine_Table_Repository($this);
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2007-01-12 01:39:32 +03: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-01-25 14:37:46 +03:00
|
|
|
public function export()
|
|
|
|
{
|
2007-01-21 21:31:51 +03:00
|
|
|
if ( ! Doctrine::isValidClassname($this->options['declaringClass']->getName())) {
|
|
|
|
throw new Doctrine_Table_Exception('Class name not valid.');
|
|
|
|
}
|
2007-01-12 01:39:32 +03:00
|
|
|
|
2007-01-21 21:31:51 +03:00
|
|
|
try {
|
|
|
|
$columns = array();
|
|
|
|
$primary = array();
|
|
|
|
|
|
|
|
foreach ($this->columns as $name => $column) {
|
2007-02-09 01:23:47 +03:00
|
|
|
$definition = $column[2];
|
|
|
|
$definition['type'] = $column[0];
|
|
|
|
$definition['length'] = $column[1];
|
2007-01-21 21:31:51 +03:00
|
|
|
|
|
|
|
switch ($definition['type']) {
|
2007-04-11 23:06:13 +04:00
|
|
|
case 'enum':
|
|
|
|
if (isset($definition['default'])) {
|
|
|
|
$definition['default'] = $this->enumIndex($name, $definition['default']);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'boolean':
|
|
|
|
if (isset($definition['default'])) {
|
|
|
|
$definition['default'] = (int) $definition['default'];
|
|
|
|
}
|
|
|
|
break;
|
2007-01-12 01:39:32 +03:00
|
|
|
}
|
2007-01-21 21:31:51 +03:00
|
|
|
$columns[$name] = $definition;
|
2007-04-11 23:06:13 +04:00
|
|
|
|
2007-01-21 21:31:51 +03:00
|
|
|
if(isset($definition['primary']) && $definition['primary']) {
|
|
|
|
$primary[] = $name;
|
|
|
|
}
|
|
|
|
}
|
2007-02-17 15:38:02 +03:00
|
|
|
|
2007-03-22 01:11:18 +03:00
|
|
|
if ($this->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_CONSTRAINTS) {
|
|
|
|
|
|
|
|
foreach ($this->getRelations() as $name => $relation) {
|
|
|
|
$fk = $relation->toArray();
|
|
|
|
$fk['foreignTable'] = $relation->getTable()->getTableName();
|
2007-04-11 23:06:13 +04:00
|
|
|
|
2007-03-27 00:58:26 +04:00
|
|
|
if ($relation->getTable() === $this && in_array($relation->getLocal(), $primary)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-03-22 01:11:18 +03:00
|
|
|
if ($relation->hasConstraint()) {
|
2007-04-11 23:06:13 +04:00
|
|
|
|
2007-03-22 01:11:18 +03:00
|
|
|
$options['foreignKeys'][] = $fk;
|
|
|
|
} elseif ($relation instanceof Doctrine_Relation_LocalKey) {
|
|
|
|
$options['foreignKeys'][] = $fk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-02-17 15:38:02 +03:00
|
|
|
|
2007-01-21 21:31:51 +03:00
|
|
|
$options['primary'] = $primary;
|
|
|
|
|
|
|
|
$this->conn->export->createTable($this->options['tableName'], $columns, array_merge($this->options, $options));
|
|
|
|
} catch(Doctrine_Connection_Exception $e) {
|
|
|
|
// we only want to silence table already exists errors
|
|
|
|
if($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
|
|
|
|
throw $e;
|
2007-01-12 01:39:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-01-25 14:37:46 +03: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()
|
|
|
|
{
|
2007-04-11 23:06:13 +04:00
|
|
|
try {
|
|
|
|
$this->conn->beginTransaction();
|
2007-01-25 14:37:46 +03:00
|
|
|
|
|
|
|
foreach ($this->options['index'] as $index => $definition) {
|
|
|
|
$this->conn->export->createIndex($this->options['tableName'], $index, $definition);
|
|
|
|
}
|
|
|
|
$this->conn->commit();
|
|
|
|
} catch(Doctrine_Connection_Exception $e) {
|
|
|
|
$this->conn->rollback();
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2007-02-08 16:56:23 +03:00
|
|
|
/**
|
|
|
|
* __get
|
|
|
|
* an alias for getOption
|
|
|
|
*
|
|
|
|
* @param string $option
|
|
|
|
*/
|
|
|
|
public function __get($option)
|
|
|
|
{
|
|
|
|
if (isset($this->options[$option])) {
|
|
|
|
return $this->options[$option];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* __isset
|
|
|
|
*
|
|
|
|
* @param string $option
|
|
|
|
*/
|
|
|
|
public function __isset($option)
|
|
|
|
{
|
|
|
|
return isset($this->options[$option]);
|
|
|
|
}
|
2007-02-17 01:54:59 +03:00
|
|
|
/**
|
|
|
|
* addForeignKey
|
|
|
|
*
|
|
|
|
* adds a foreignKey to this table
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addForeignKey(array $definition)
|
|
|
|
{
|
|
|
|
$this->options['foreignKeys'][] = $definition;
|
|
|
|
}
|
2007-02-12 01:53:35 +03:00
|
|
|
/**
|
|
|
|
* addIndex
|
|
|
|
*
|
|
|
|
* adds an index to this table
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addIndex($index, array $definition)
|
|
|
|
{
|
2007-04-11 23:06:13 +04:00
|
|
|
$index = $this->conn->getIndexName($index);
|
2007-02-12 01:53:35 +03:00
|
|
|
$this->options['indexes'][$index] = $definition;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getIndex
|
|
|
|
*
|
|
|
|
* @return array|boolean array on success, FALSE on failure
|
|
|
|
*/
|
|
|
|
public function getIndex($index)
|
|
|
|
{
|
|
|
|
if (isset($this->options['indexes'][$index])) {
|
|
|
|
return $this->options['indexes'][$index];
|
|
|
|
}
|
2007-04-11 23:06:13 +04:00
|
|
|
|
2007-02-12 01:53:35 +03:00
|
|
|
return false;
|
|
|
|
}
|
2006-11-06 21:25:39 +03:00
|
|
|
/**
|
|
|
|
* createQuery
|
|
|
|
* creates a new Doctrine_Query object and adds the component name
|
|
|
|
* of this table as the query 'from' part
|
2006-12-29 17:01:31 +03:00
|
|
|
*
|
2006-11-06 21:25:39 +03:00
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function createQuery()
|
|
|
|
{
|
2006-11-06 21:25:39 +03:00
|
|
|
return Doctrine_Query::create()->from($this->getComponentName());
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
2006-09-30 13:54:12 +04:00
|
|
|
* getRepository
|
|
|
|
*
|
|
|
|
* @return Doctrine_Table_Repository
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getRepository()
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
return $this->repository;
|
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
|
2006-12-29 17:40:47 +03:00
|
|
|
public function setOption($name, $value)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
switch ($name) {
|
2007-04-11 23:06:13 +04:00
|
|
|
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;
|
2006-10-24 21:02:47 +04:00
|
|
|
}
|
|
|
|
$this->options[$name] = $value;
|
|
|
|
}
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getOption($name)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($this->options[$name])) {
|
2006-11-05 22:24:28 +03:00
|
|
|
return $this->options[$name];
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-11-05 22:24:28 +03:00
|
|
|
return null;
|
|
|
|
}
|
2007-01-23 19:27:20 +03: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
|
|
|
|
*/
|
|
|
|
public function getColumnName($alias)
|
|
|
|
{
|
|
|
|
if(isset($this->columnAliases[$alias])) {
|
|
|
|
return $this->columnAliases[$alias];
|
|
|
|
}
|
2007-04-11 23:06:13 +04:00
|
|
|
|
2007-01-23 19:27:20 +03:00
|
|
|
return $alias;
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* setColumn
|
2007-01-10 23:36:29 +03:00
|
|
|
*
|
2006-05-30 12:42:10 +04:00
|
|
|
* @param string $name
|
|
|
|
* @param string $type
|
|
|
|
* @param integer $length
|
|
|
|
* @param mixed $options
|
2007-01-10 23:36:29 +03:00
|
|
|
* @throws Doctrine_Table_Exception if trying use wrongly typed parameter
|
2006-05-30 12:42:10 +04:00
|
|
|
* @return void
|
|
|
|
*/
|
2007-01-23 19:27:20 +03:00
|
|
|
public function setColumn($name, $type, $length = null, $options = array())
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if (is_string($options)) {
|
2006-08-31 14:00:09 +04:00
|
|
|
$options = explode('|', $options);
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-01-10 23:36:29 +03:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
foreach ($options as $k => $option) {
|
|
|
|
if (is_numeric($k)) {
|
|
|
|
if ( ! empty($option)) {
|
2006-09-16 00:56:25 +04:00
|
|
|
$options[$option] = true;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-09-16 00:56:25 +04:00
|
|
|
unset($options[$k]);
|
|
|
|
}
|
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
|
2007-01-23 19:27:20 +03:00
|
|
|
$name = strtolower($name);
|
|
|
|
$parts = explode(' as ', $name);
|
2007-04-11 23:06:13 +04:00
|
|
|
|
2007-01-23 19:27:20 +03:00
|
|
|
if (count($parts) > 1) {
|
|
|
|
$this->columnAliases[$parts[1]] = $parts[0];
|
|
|
|
$name = $parts[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($length == null) {
|
2006-12-24 01:48:16 +03:00
|
|
|
$length = 2147483647;
|
2007-01-23 19:27:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((string) (int) $length !== (string) $length) {
|
2007-01-10 23:36:29 +03:00
|
|
|
throw new Doctrine_Table_Exception('Invalid argument given for column length');
|
|
|
|
}
|
2006-12-24 01:48:16 +03:00
|
|
|
|
2007-02-09 01:23:47 +03:00
|
|
|
$this->columns[$name] = array($type, $length, $options);
|
2006-07-26 21:17:59 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($options['primary'])) {
|
2006-05-30 12:42:10 +04:00
|
|
|
$this->primaryKeys[] = $name;
|
|
|
|
}
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($options['default'])) {
|
2006-09-16 00:56:25 +04:00
|
|
|
$this->hasDefaultValues = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* hasDefaultValues
|
|
|
|
* returns true if this table has default values, otherwise false
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function hasDefaultValues()
|
|
|
|
{
|
2006-09-16 00:56:25 +04:00
|
|
|
return $this->hasDefaultValues;
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2007-02-09 01:23:47 +03:00
|
|
|
/**
|
|
|
|
* getDefaultValueOf
|
|
|
|
* returns the default value(if any) for given column
|
|
|
|
*
|
|
|
|
* @param string $column
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getDefaultValueOf($column)
|
|
|
|
{
|
|
|
|
$column = strtolower($column);
|
|
|
|
if ( ! isset($this->columns[$column])) {
|
|
|
|
throw new Doctrine_Table_Exception("Couldn't get default value. Column ".$column." doesn't exist.");
|
|
|
|
}
|
|
|
|
if (isset($this->columns[$column][2]['default'])) {
|
|
|
|
return $this->columns[$column][2]['default'];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function getIdentifier()
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
return $this->identifier;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return integer
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function getIdentifierType()
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
return $this->identifierType;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* hasColumn
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function hasColumn($name)
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
return isset($this->columns[$name]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param mixed $key
|
|
|
|
* @return void
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function setPrimaryKey($key)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
switch (gettype($key)) {
|
2007-04-11 23:06:13 +04:00
|
|
|
case "array":
|
|
|
|
$this->primaryKeys = array_values($key);
|
|
|
|
break;
|
|
|
|
case "string":
|
|
|
|
$this->primaryKeys[] = $key;
|
|
|
|
break;
|
2006-12-29 17:01:31 +03:00
|
|
|
};
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns all primary keys
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function getPrimaryKeys()
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
return $this->primaryKeys;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function hasPrimaryKey($key)
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
return in_array($key,$this->primaryKeys);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns all bound relations
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getBounds()
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
return $this->bound;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns a bound relation array
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getBound($name)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! isset($this->bound[$name])) {
|
2007-04-03 21:36:46 +04:00
|
|
|
throw new Doctrine_Table_Exception('Unknown bound ' . $name);
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
return $this->bound[$name];
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns a bound relation array
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getBoundForName($name, $component)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
foreach ($this->bound as $k => $bound) {
|
2007-02-17 01:54:59 +03:00
|
|
|
$e = explode('.', $bound['field']);
|
2006-10-27 00:53:59 +04:00
|
|
|
|
2007-02-17 15:38:02 +03:00
|
|
|
if ($bound['class'] == $name && $e[0] == $component) {
|
2006-05-30 12:42:10 +04:00
|
|
|
return $this->bound[$k];
|
|
|
|
}
|
|
|
|
}
|
2007-02-17 15:38:02 +03:00
|
|
|
throw new Doctrine_Table_Exception('Unknown bound ' . $name);
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns the alias for given component name
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getAlias($name)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($this->boundAliases[$name])) {
|
2006-05-30 12:42:10 +04:00
|
|
|
return $this->boundAliases[$name];
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns component name for given alias
|
2006-07-26 21:17:59 +04:00
|
|
|
*
|
2006-05-30 12:42:10 +04:00
|
|
|
* @param string $alias
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getAliasName($alias)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($name = array_search($alias, $this->boundAliases)) {
|
2006-05-30 12:42:10 +04:00
|
|
|
return $name;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-11-11 22:51:51 +03:00
|
|
|
return $alias;
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* unbinds all relations
|
2006-07-26 21:17:59 +04:00
|
|
|
*
|
2006-05-30 12:42:10 +04:00
|
|
|
* @return void
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function unbindAll()
|
2007-04-03 21:36:46 +04:00
|
|
|
{ throw new Exception();
|
2007-04-11 23:06:13 +04:00
|
|
|
$this->bound = array();
|
|
|
|
$this->relations = array();
|
|
|
|
$this->boundAliases = array();
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* unbinds a relation
|
|
|
|
* returns true on success, false on failure
|
|
|
|
*
|
|
|
|
* @param $name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function unbind($name)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! isset($this->bound[$name])) {
|
2006-05-30 12:42:10 +04:00
|
|
|
return false;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
unset($this->bound[$name]);
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($this->relations[$name])) {
|
2006-05-30 12:42:10 +04:00
|
|
|
unset($this->relations[$name]);
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
if (isset($this->boundAliases[$name])) {
|
2006-05-30 12:42:10 +04:00
|
|
|
unset($this->boundAliases[$name]);
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* binds a relation
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $field
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-02-17 15:38:02 +03:00
|
|
|
public function bind($name, $field, $type, $options = null)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($this->relations[$name])) {
|
2006-10-26 01:12:30 +04:00
|
|
|
unset($this->relations[$name]);
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-02-06 22:06:17 +03:00
|
|
|
|
|
|
|
$lower = strtolower($name);
|
|
|
|
|
|
|
|
if (isset($this->columns[$lower])) {
|
2007-02-06 22:07:31 +03:00
|
|
|
throw new Doctrine_Table_Exception("Couldn't bind relation. Column with name " . $lower . ' already exists!');
|
2007-02-06 22:06:17 +03:00
|
|
|
}
|
|
|
|
|
2007-02-17 01:54:59 +03:00
|
|
|
$e = explode(' as ', $name);
|
|
|
|
$name = $e[0];
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($e[1])) {
|
2006-05-30 12:42:10 +04:00
|
|
|
$alias = $e[1];
|
|
|
|
$this->boundAliases[$name] = $alias;
|
2006-12-29 17:01:31 +03:00
|
|
|
} else {
|
2006-05-30 12:42:10 +04:00
|
|
|
$alias = $name;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2007-02-17 01:54:59 +03:00
|
|
|
$this->bound[$alias] = array('field' => $field,
|
2007-04-11 23:06:13 +04:00
|
|
|
'type' => $type,
|
|
|
|
'class' => $name,
|
|
|
|
'alias' => $alias);
|
2007-02-17 15:38:02 +03:00
|
|
|
if ($options !== null) {
|
|
|
|
$opt = array();
|
|
|
|
if (is_string($options)) {
|
|
|
|
$opt['local'] = $options;
|
|
|
|
} else {
|
|
|
|
$opt = (array) $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->bound[$alias] = array_merge($this->bound[$alias], $opt);
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
/**
|
2006-08-22 03:21:01 +04:00
|
|
|
* @return Doctrine_Connection
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getConnection()
|
|
|
|
{
|
2006-12-01 01:33:54 +03:00
|
|
|
return $this->conn;
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2006-07-26 21:17:59 +04:00
|
|
|
/**
|
|
|
|
* hasRelatedComponent
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function hasRelatedComponent($name, $component)
|
|
|
|
{
|
2007-04-11 23:06:13 +04:00
|
|
|
return (strpos($this->bound[$name]['field'], $component . '.') !== false);
|
2006-07-26 21:17:59 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param string $name component name of which a foreign key object is bound
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function hasRelation($name)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($this->bound[$name])) {
|
2006-07-26 21:17:59 +04:00
|
|
|
return true;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
|
|
|
foreach ($this->bound as $k=>$v) {
|
|
|
|
if ($this->hasRelatedComponent($k, $name)) {
|
2006-07-26 21:17:59 +04:00
|
|
|
return true;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-07-26 21:17:59 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
2006-10-01 14:10:11 +04:00
|
|
|
* getRelation
|
|
|
|
*
|
2006-05-30 12:42:10 +04:00
|
|
|
* @param string $name component name of which a foreign key object is bound
|
|
|
|
* @return Doctrine_Relation
|
|
|
|
*/
|
2007-02-17 01:54:59 +03:00
|
|
|
public function getRelation($name, $recursive = true)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($this->relations[$name])) {
|
|
|
|
return $this->relations[$name];
|
|
|
|
}
|
2007-04-03 21:36:46 +04:00
|
|
|
|
|
|
|
if ( ! $this->conn->hasTable($this->options['name'])) {
|
|
|
|
$allowExport = true;
|
|
|
|
} else {
|
|
|
|
$allowExport = false;
|
|
|
|
}
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($this->bound[$name])) {
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2007-02-17 01:54:59 +03:00
|
|
|
$definition = $this->bound[$name];
|
|
|
|
|
|
|
|
list($component, $definition['foreign']) = explode('.', $definition['field']);
|
|
|
|
unset($definition['field']);
|
|
|
|
|
2007-04-03 21:36:46 +04:00
|
|
|
$definition['table'] = $this->conn->getTable($definition['class'], $allowExport);
|
2007-03-22 01:11:18 +03:00
|
|
|
$definition['constraint'] = false;
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2007-02-17 01:54:59 +03:00
|
|
|
if ($component == $this->options['name'] || in_array($component, $this->options['parents'])) {
|
2007-02-08 01:54:03 +03:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
// ONE-TO-ONE
|
2007-02-17 01:54:59 +03:00
|
|
|
if ($definition['type'] == Doctrine_Relation::ONE_COMPOSITE ||
|
|
|
|
$definition['type'] == Doctrine_Relation::ONE_AGGREGATE) {
|
2007-04-11 23:06:13 +04:00
|
|
|
// tree structure parent relation found
|
2007-02-17 01:54:59 +03:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
if ( ! isset($definition['local'])) {
|
|
|
|
$definition['local'] = $definition['foreign'];
|
|
|
|
$definition['foreign'] = $definition['table']->getIdentifier();
|
|
|
|
}
|
2007-02-17 01:54:59 +03:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
$relation = new Doctrine_Relation_LocalKey($definition);
|
2007-01-05 00:08:56 +03:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
} else {
|
|
|
|
// tree structure children relation found
|
2007-02-17 15:38:02 +03:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
if ( ! isset($definition['local'])) {
|
|
|
|
$tmp = $definition['table']->getIdentifier();
|
2007-01-05 00:08:56 +03:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
$definition['local'] = $tmp;
|
|
|
|
}
|
2006-10-23 20:19:47 +04:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
//$definition['foreign'] = $tmp;
|
|
|
|
$definition['constraint'] = true;
|
2006-09-19 00:11:21 +04:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
$relation = new Doctrine_Relation_ForeignKey($definition);
|
|
|
|
}
|
2007-03-22 01:11:18 +03:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
} elseif ($component == $definition['class'] ||
|
|
|
|
($component == $definition['alias'])) { // && ($name == $this->options['name'] || in_array($name,$this->parents))
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
if ( ! isset($defintion['local'])) {
|
|
|
|
$definition['local'] = $this->identifier;
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
$definition['constraint'] = true;
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
// ONE-TO-MANY or ONE-TO-ONE
|
|
|
|
$relation = new Doctrine_Relation_ForeignKey($definition);
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
} else {
|
|
|
|
// MANY-TO-MANY
|
|
|
|
// only aggregate relations allowed
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
if ($definition['type'] != Doctrine_Relation::MANY_AGGREGATE) {
|
|
|
|
throw new Doctrine_Table_Exception("Only aggregate relations are allowed for many-to-many relations");
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
$classes = array_merge($this->options['parents'], array($this->options['name']));
|
2006-07-26 21:17:59 +04:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
foreach (array_reverse($classes) as $class) {
|
|
|
|
try {
|
|
|
|
$bound = $definition['table']->getBoundForName($class, $component);
|
|
|
|
break;
|
|
|
|
} catch(Doctrine_Table_Exception $exc) { }
|
|
|
|
}
|
|
|
|
if ( ! isset($bound)) {
|
|
|
|
throw new Doctrine_Table_Exception("Couldn't map many-to-many relation for "
|
|
|
|
. $this->options['name'] . " and $name. Components use different join tables.");
|
|
|
|
}
|
|
|
|
if ( ! isset($definition['local'])) {
|
|
|
|
$definition['local'] = $this->identifier;
|
|
|
|
}
|
|
|
|
$e2 = explode('.', $bound['field']);
|
|
|
|
$fields = explode('-', $e2[1]);
|
2007-02-17 01:54:59 +03:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
if ($e2[0] != $component) {
|
|
|
|
throw new Doctrine_Table_Exception($e2[0] . ' doesn\'t match ' . $component);
|
|
|
|
}
|
|
|
|
$associationTable = $this->conn->getTable($e2[0], $allowExport);
|
2007-02-17 01:54:59 +03:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
if (count($fields) > 1) {
|
|
|
|
// SELF-REFERENCING THROUGH JOIN TABLE
|
2007-02-17 01:54:59 +03:00
|
|
|
|
|
|
|
$def['table'] = $associationTable;
|
2007-04-11 23:06:13 +04:00
|
|
|
$def['local'] = $this->identifier;
|
|
|
|
$def['foreign'] = $fields[0];
|
2007-02-17 01:54:59 +03:00
|
|
|
$def['alias'] = $e2[0];
|
2007-02-17 15:38:02 +03:00
|
|
|
$def['class'] = $e2[0];
|
2007-02-17 01:54:59 +03:00
|
|
|
$def['type'] = Doctrine_Relation::MANY_COMPOSITE;
|
2007-04-11 23:06:13 +04:00
|
|
|
|
2007-02-17 01:54:59 +03:00
|
|
|
$this->relations[$e2[0]] = new Doctrine_Relation_ForeignKey($def);
|
2007-04-11 23:06:13 +04:00
|
|
|
|
2007-02-17 01:54:59 +03:00
|
|
|
$definition['assocTable'] = $associationTable;
|
2007-04-11 23:06:13 +04:00
|
|
|
$definition['local'] = $fields[0];
|
|
|
|
$definition['foreign'] = $fields[1];
|
|
|
|
$relation = new Doctrine_Relation_Association_Self($definition);
|
|
|
|
} else {
|
|
|
|
if($definition['table'] === $this) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// auto initialize a new one-to-one relationships for association table
|
|
|
|
$associationTable->bind($this->getComponentName(),
|
|
|
|
$associationTable->getComponentName(). '.' . $e2[1],
|
|
|
|
Doctrine_Relation::ONE_AGGREGATE
|
|
|
|
);
|
|
|
|
|
|
|
|
$associationTable->bind($definition['table']->getComponentName(),
|
|
|
|
$associationTable->getComponentName(). '.' . $definition['foreign'],
|
|
|
|
Doctrine_Relation::ONE_AGGREGATE
|
|
|
|
);
|
|
|
|
|
|
|
|
// NORMAL MANY-TO-MANY RELATIONSHIP
|
|
|
|
|
|
|
|
$def['table'] = $associationTable;
|
|
|
|
$def['foreign'] = $e2[1];
|
|
|
|
$def['local'] = $definition['local'];
|
|
|
|
$def['alias'] = $e2[0];
|
|
|
|
$def['class'] = $e2[0];
|
|
|
|
$def['type'] = Doctrine_Relation::MANY_COMPOSITE;
|
|
|
|
$this->relations[$e2[0]] = new Doctrine_Relation_ForeignKey($def);
|
|
|
|
|
|
|
|
$definition['local'] = $e2[1];
|
|
|
|
$definition['assocTable'] = $associationTable;
|
|
|
|
$relation = new Doctrine_Relation_Association($definition);
|
|
|
|
}
|
2007-02-08 01:54:03 +03:00
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2006-10-20 22:21:42 +04:00
|
|
|
|
2007-02-17 01:54:59 +03:00
|
|
|
$this->relations[$name] = $relation;
|
|
|
|
|
|
|
|
return $this->relations[$name];
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2007-04-11 23:06:13 +04:00
|
|
|
|
2006-10-20 22:21:42 +04:00
|
|
|
|
2006-10-01 14:10:11 +04:00
|
|
|
// load all relations
|
|
|
|
$this->getRelations();
|
2006-12-29 17:01:31 +03:00
|
|
|
|
|
|
|
if ($recursive) {
|
2007-02-17 01:54:59 +03:00
|
|
|
return $this->getRelation($name, false);
|
2006-10-01 14:10:11 +04:00
|
|
|
} else {
|
2007-02-17 01:54:59 +03:00
|
|
|
throw new Doctrine_Table_Exception($this->options['name'] . " doesn't have a relation to " . $name);
|
2006-09-17 19:44:10 +04:00
|
|
|
}
|
2007-02-17 01:54:59 +03:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns an array containing all foreign key objects
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function getRelations()
|
|
|
|
{
|
2007-02-17 15:38:02 +03:00
|
|
|
foreach ($this->bound as $k => $v) {
|
2006-08-31 13:04:14 +04:00
|
|
|
$this->getRelation($k);
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->relations;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* create
|
|
|
|
* creates a new record
|
|
|
|
*
|
|
|
|
* @param $array an array where keys are field names and values representing field values
|
|
|
|
* @return Doctrine_Record
|
|
|
|
*/
|
|
|
|
public function create(array $array = array()) {
|
2006-07-26 21:17:59 +04:00
|
|
|
$this->data = $array;
|
2006-11-06 21:34:10 +03:00
|
|
|
$record = new $this->options['name']($this, true);
|
2006-05-30 12:42:10 +04:00
|
|
|
$this->data = array();
|
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* finds a record by its identifier
|
|
|
|
*
|
|
|
|
* @param $id database row id
|
2006-10-17 21:21:21 +04:00
|
|
|
* @return Doctrine_Record|false a record for given database identifier
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function find($id)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($id !== null) {
|
|
|
|
if ( ! is_array($id)) {
|
2006-05-30 12:42:10 +04:00
|
|
|
$id = array($id);
|
2006-12-29 17:01:31 +03:00
|
|
|
} else {
|
2006-05-30 12:42:10 +04:00
|
|
|
$id = array_values($id);
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2007-01-21 21:31:51 +03:00
|
|
|
$query = $this->query . ' WHERE ' . implode(' = ? AND ', $this->primaryKeys) . ' = ?';
|
2006-05-30 12:42:10 +04:00
|
|
|
$query = $this->applyInheritance($query);
|
2006-06-03 13:10:43 +04:00
|
|
|
|
2006-10-24 21:02:47 +04:00
|
|
|
$params = array_merge($id, array_values($this->options['inheritanceMap']));
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2007-01-21 21:31:51 +03:00
|
|
|
$stmt = $this->conn->execute($query, $params);
|
2006-06-03 13:10:43 +04:00
|
|
|
|
|
|
|
$this->data = $stmt->fetch(PDO::FETCH_ASSOC);
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($this->data === false)
|
2006-06-07 00:37:56 +04:00
|
|
|
return false;
|
2006-12-29 17:01:31 +03:00
|
|
|
|
2006-10-15 23:19:20 +04:00
|
|
|
return $this->getRecord();
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2006-10-15 23:19:20 +04:00
|
|
|
return false;
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* applyInheritance
|
|
|
|
* @param $where query where part to be modified
|
|
|
|
* @return string query where part with column aggregation inheritance added
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function applyInheritance($where)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! empty($this->options['inheritanceMap'])) {
|
2006-05-30 12:42:10 +04:00
|
|
|
$a = array();
|
2006-12-29 17:01:31 +03:00
|
|
|
foreach ($this->options['inheritanceMap'] as $field => $value) {
|
2007-01-21 21:31:51 +03:00
|
|
|
$a[] = $field . ' = ?';
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2007-01-21 21:31:51 +03:00
|
|
|
$i = implode(' AND ', $a);
|
|
|
|
$where .= ' AND ' . $i;
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
return $where;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* findAll
|
|
|
|
* returns a collection of records
|
|
|
|
*
|
|
|
|
* @return Doctrine_Collection
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function findAll()
|
|
|
|
{
|
2006-12-01 01:33:54 +03:00
|
|
|
$graph = new Doctrine_Query($this->conn);
|
2006-10-24 21:02:47 +04:00
|
|
|
$users = $graph->query("FROM ".$this->options['name']);
|
2006-05-30 12:42:10 +04:00
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
/**
|
2006-08-07 13:55:46 +04:00
|
|
|
* findByDql
|
|
|
|
* finds records with given DQL where clause
|
2006-05-30 12:42:10 +04:00
|
|
|
* returns a collection of records
|
|
|
|
*
|
2006-08-07 13:55:46 +04:00
|
|
|
* @param string $dql DQL after WHERE clause
|
2006-05-30 12:42:10 +04:00
|
|
|
* @param array $params query parameters
|
|
|
|
* @return Doctrine_Collection
|
|
|
|
*/
|
2006-08-07 13:55:46 +04:00
|
|
|
public function findBySql($dql, array $params = array()) {
|
2006-12-01 01:33:54 +03:00
|
|
|
$q = new Doctrine_Query($this->conn);
|
2006-10-24 21:02:47 +04:00
|
|
|
$users = $q->query("FROM ".$this->options['name']." WHERE ".$dql, $params);
|
2006-05-30 12:42:10 +04:00
|
|
|
return $users;
|
|
|
|
}
|
2006-08-20 22:52:07 +04:00
|
|
|
|
2006-08-17 13:42:18 +04:00
|
|
|
public function findByDql($dql, array $params = array()) {
|
|
|
|
return $this->findBySql($dql, $params);
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* clear
|
|
|
|
* clears the first level cache (identityMap)
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function clear()
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
$this->identityMap = array();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getRecord
|
|
|
|
* first checks if record exists in identityMap, if not
|
|
|
|
* returns a new record
|
|
|
|
*
|
|
|
|
* @return Doctrine_Record
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getRecord()
|
|
|
|
{
|
2006-09-03 23:20:02 +04:00
|
|
|
$this->data = array_change_key_case($this->data, CASE_LOWER);
|
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
$key = $this->getIdentifier();
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! is_array($key)) {
|
2006-05-30 12:42:10 +04:00
|
|
|
$key = array($key);
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2007-03-22 16:32:43 +03:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
foreach ($key as $k) {
|
|
|
|
if ( ! isset($this->data[$k])) {
|
2007-03-22 16:32:43 +03:00
|
|
|
throw new Doctrine_Table_Exception("Primary key value for $k wasn't found");
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
$id[] = $this->data[$k];
|
|
|
|
}
|
2006-06-01 15:58:05 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
$id = implode(' ', $id);
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (isset($this->identityMap[$id])) {
|
2006-05-30 12:42:10 +04:00
|
|
|
$record = $this->identityMap[$id];
|
2006-12-29 17:01:31 +03:00
|
|
|
} else {
|
2007-04-11 23:06:13 +04:00
|
|
|
$recordName = $this->getClassnameToReturn();
|
|
|
|
$record = new $recordName($this);
|
2006-05-30 12:42:10 +04:00
|
|
|
$this->identityMap[$id] = $record;
|
|
|
|
}
|
|
|
|
$this->data = array();
|
|
|
|
|
|
|
|
return $record;
|
|
|
|
}
|
2007-04-08 18:55:14 +04:00
|
|
|
|
2007-04-11 23:06:13 +04:00
|
|
|
/**
|
|
|
|
* Get the classname to return. Most often this is just the options['name']
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* return the name of the class for this tables record.
|
|
|
|
*
|
|
|
|
* @todo this function could use reflection to check the first time it runs
|
|
|
|
* if the subclassing option is not set.
|
|
|
|
*
|
|
|
|
* @return string The name of the class to create
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getClassnameToReturn()
|
|
|
|
{
|
2007-04-11 23:12:38 +04:00
|
|
|
if (!isset($this->options['subclasses'])) {
|
2007-04-11 23:06:13 +04:00
|
|
|
return $this->options['name'];
|
|
|
|
}
|
2007-04-11 23:12:38 +04:00
|
|
|
foreach ($this->options['subclasses'] as $subclass) {
|
2007-04-11 23:06:13 +04:00
|
|
|
$table = $this->conn->getTable($subclass);
|
2007-04-11 23:12:38 +04:00
|
|
|
$inheritanceMap = $table->getOption('inheritanceMap');
|
2007-04-11 23:06:13 +04:00
|
|
|
$nomatch = false;
|
|
|
|
foreach ($inheritanceMap as $key => $value) {
|
|
|
|
if (!isset($this->data[$key]) || $this->data[$key] != $value) {
|
|
|
|
$nomatch = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$nomatch) {
|
|
|
|
return $table->getComponentName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->options['name'];
|
|
|
|
}
|
2007-04-08 18:55:14 +04:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* @param $id database row id
|
|
|
|
* @throws Doctrine_Find_Exception
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function getProxy($id = null)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($id !== null) {
|
2007-01-21 21:31:51 +03:00
|
|
|
$query = 'SELECT ' . implode(', ',$this->primaryKeys)
|
2007-04-11 23:06:13 +04:00
|
|
|
. ' FROM ' . $this->getTableName()
|
|
|
|
. ' WHERE ' . implode(' = ? && ',$this->primaryKeys).' = ?';
|
2006-05-30 12:42:10 +04:00
|
|
|
$query = $this->applyInheritance($query);
|
2006-07-26 21:17:59 +04:00
|
|
|
|
2006-10-24 21:02:47 +04:00
|
|
|
$params = array_merge(array($id), array_values($this->options['inheritanceMap']));
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2006-12-01 01:33:54 +03:00
|
|
|
$this->data = $this->conn->execute($query,$params)->fetch(PDO::FETCH_ASSOC);
|
2006-05-30 12:42:10 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if ($this->data === false)
|
2006-08-31 14:00:09 +04:00
|
|
|
return false;
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
return $this->getRecord();
|
|
|
|
}
|
2006-07-24 01:56:21 +04:00
|
|
|
/**
|
|
|
|
* count
|
2006-08-21 19:30:18 +04:00
|
|
|
*
|
2006-07-24 01:56:21 +04:00
|
|
|
* @return integer
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function count()
|
|
|
|
{
|
2006-12-01 01:33:54 +03:00
|
|
|
$a = $this->conn->getDBH()->query("SELECT COUNT(1) FROM ".$this->options['tableName'])->fetch(PDO::FETCH_NUM);
|
2006-07-24 01:56:21 +04:00
|
|
|
return current($a);
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* @return Doctrine_Query a Doctrine_Query object
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getQueryObject()
|
|
|
|
{
|
2006-08-22 03:21:01 +04:00
|
|
|
$graph = new Doctrine_Query($this->getConnection());
|
2006-05-30 12:42:10 +04:00
|
|
|
$graph->load($this->getComponentName());
|
|
|
|
return $graph;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* execute
|
|
|
|
* @param string $query
|
|
|
|
* @param array $array
|
|
|
|
* @param integer $limit
|
|
|
|
* @param integer $offset
|
|
|
|
*/
|
|
|
|
public function execute($query, array $array = array(), $limit = null, $offset = null) {
|
|
|
|
$coll = new Doctrine_Collection($this);
|
2006-12-01 01:33:54 +03:00
|
|
|
$query = $this->conn->modifyLimitQuery($query,$limit,$offset);
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! empty($array)) {
|
2006-12-01 01:33:54 +03:00
|
|
|
$stmt = $this->conn->getDBH()->prepare($query);
|
2006-05-30 12:42:10 +04:00
|
|
|
$stmt->execute($array);
|
|
|
|
} else {
|
2006-12-01 01:33:54 +03:00
|
|
|
$stmt = $this->conn->getDBH()->query($query);
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
|
|
|
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$stmt->closeCursor();
|
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
foreach ($data as $row) {
|
2006-05-30 12:42:10 +04:00
|
|
|
$this->data = $row;
|
|
|
|
$record = $this->getRecord();
|
|
|
|
$coll->add($record);
|
|
|
|
}
|
|
|
|
return $coll;
|
|
|
|
}
|
2006-06-20 01:31:22 +04:00
|
|
|
/**
|
|
|
|
* @param string $field
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function getEnumValues($field)
|
|
|
|
{
|
2007-02-09 01:23:47 +03:00
|
|
|
if (isset($this->columns[$field][2]['values'])) {
|
|
|
|
return $this->columns[$field][2]['values'];
|
2006-12-29 17:01:31 +03:00
|
|
|
} else {
|
2006-06-20 01:31:22 +04:00
|
|
|
return array();
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-06-20 01:31:22 +04:00
|
|
|
}
|
2006-06-18 02:46:03 +04:00
|
|
|
/**
|
|
|
|
* enumValue
|
2006-09-17 19:44:10 +04:00
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @param integer $index
|
|
|
|
* @return mixed
|
2006-06-18 02:46:03 +04:00
|
|
|
*/
|
2007-01-21 21:31:51 +03:00
|
|
|
public function enumValue($field, $index)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2006-10-25 09:46:06 +04:00
|
|
|
if ($index instanceof Doctrine_Null)
|
2006-10-26 00:02:40 +04:00
|
|
|
return $index;
|
2006-10-26 01:12:30 +04:00
|
|
|
|
2007-02-09 01:23:47 +03:00
|
|
|
return isset($this->columns[$field][2]['values'][$index]) ? $this->columns[$field][2]['values'][$index] : $index;
|
2007-01-21 21:31:51 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* enumIndex
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @param mixed $value
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function enumIndex($field, $value)
|
|
|
|
{
|
2007-01-29 17:44:27 +03:00
|
|
|
$values = $this->getEnumValues($field);
|
2007-01-21 21:31:51 +03:00
|
|
|
|
|
|
|
return array_search($value, $values);
|
2006-06-18 02:46:03 +04:00
|
|
|
}
|
2006-10-06 20:50:00 +04:00
|
|
|
/**
|
|
|
|
* invokeSet
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function invokeSet(Doctrine_Record $record, $name, $value)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! ($this->getAttribute(Doctrine::ATTR_ACCESSORS) & Doctrine::ACCESSOR_SET)) {
|
2006-12-24 01:48:16 +03:00
|
|
|
return $value;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-12-24 01:48:16 +03:00
|
|
|
$prefix = $this->getAttribute(Doctrine::ATTR_ACCESSOR_PREFIX_SET);
|
|
|
|
if (!$prefix)
|
2006-12-14 22:01:24 +03:00
|
|
|
$prefix = 'set';
|
2006-10-06 20:50:00 +04:00
|
|
|
|
2006-12-14 22:01:24 +03:00
|
|
|
$method = $prefix . $name;
|
2006-10-06 20:50:00 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (method_exists($record, $method)) {
|
2006-10-06 20:50:00 +04:00
|
|
|
return $record->$method($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* invokeGet
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function invokeGet(Doctrine_Record $record, $name, $value)
|
|
|
|
{
|
2006-12-29 17:01:31 +03:00
|
|
|
if ( ! ($this->getAttribute(Doctrine::ATTR_ACCESSORS) & Doctrine::ACCESSOR_GET)) {
|
2006-10-06 20:50:00 +04:00
|
|
|
return $value;
|
2006-12-29 17:01:31 +03:00
|
|
|
}
|
2006-12-24 01:48:16 +03:00
|
|
|
$prefix = $this->getAttribute(Doctrine::ATTR_ACCESSOR_PREFIX_GET);
|
|
|
|
if (!$prefix)
|
2006-12-14 22:01:24 +03:00
|
|
|
$prefix = 'get';
|
2006-12-24 01:48:16 +03:00
|
|
|
|
2006-12-14 22:01:24 +03:00
|
|
|
$method = $prefix . $name;
|
2006-10-06 20:50:00 +04:00
|
|
|
|
2006-12-29 17:01:31 +03:00
|
|
|
if (method_exists($record, $method)) {
|
2006-10-06 20:50:00 +04:00
|
|
|
return $record->$method($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
2007-02-09 01:23:47 +03:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
2007-02-09 01:23:47 +03:00
|
|
|
* getDefinitionOf
|
2006-10-06 20:50:00 +04:00
|
|
|
*
|
2007-02-09 01:23:47 +03:00
|
|
|
* @return string ValueWrapper class name on success, false on failure
|
2006-10-06 20:50:00 +04:00
|
|
|
*/
|
2007-02-09 01:23:47 +03:00
|
|
|
public function getValueWrapperOf($column)
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2007-02-09 01:23:47 +03:00
|
|
|
if (isset($this->columns[$column][2]['wrapper'])) {
|
|
|
|
return $this->columns[$column][2]['wrapper'];
|
|
|
|
}
|
|
|
|
return false;
|
2006-10-06 20:50:00 +04:00
|
|
|
}
|
|
|
|
/**
|
2007-02-09 01:23:47 +03:00
|
|
|
* getColumnCount
|
2006-10-06 20:50:00 +04:00
|
|
|
*
|
2007-02-09 01:23:47 +03:00
|
|
|
* @return integer the number of columns in this table
|
2006-05-30 12:42:10 +04:00
|
|
|
*/
|
2007-02-09 01:23:47 +03:00
|
|
|
final public function getColumnCount()
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2007-02-09 01:23:47 +03:00
|
|
|
return $this->columnCount;
|
2006-05-30 12:42:10 +04:00
|
|
|
}
|
2007-02-09 01:23:47 +03:00
|
|
|
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* returns all columns and their definitions
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2007-02-09 01:23:47 +03:00
|
|
|
final public function getColumns()
|
2006-12-29 17:40:47 +03:00
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
return $this->columns;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns an array containing all the column names
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function getColumnNames()
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
return array_keys($this->columns);
|
|
|
|
}
|
2007-02-09 01:23:47 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getDefinitionOf
|
|
|
|
*
|
|
|
|
* @return mixed array on success, false on failure
|
|
|
|
*/
|
|
|
|
public function getDefinitionOf($column)
|
|
|
|
{
|
|
|
|
if (isset($this->columns[$column])) {
|
|
|
|
return $this->columns[$column];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getTypeOf
|
|
|
|
*
|
|
|
|
* @return mixed string on success, false on failure
|
|
|
|
*/
|
|
|
|
public function getTypeOf($column)
|
|
|
|
{
|
|
|
|
if (isset($this->columns[$column])) {
|
|
|
|
return $this->columns[$column][0];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* setData
|
|
|
|
* doctrine uses this function internally
|
|
|
|
* users are strongly discouraged to use this function
|
|
|
|
*
|
|
|
|
* @param array $data internal data
|
|
|
|
* @return void
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function setData(array $data)
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns the maximum primary key value
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function getMaxIdentifier()
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
$sql = "SELECT MAX(".$this->getIdentifier().") FROM ".$this->getTableName();
|
2006-12-01 01:33:54 +03:00
|
|
|
$stmt = $this->conn->getDBH()->query($sql);
|
2006-05-30 12:42:10 +04:00
|
|
|
$data = $stmt->fetch(PDO::FETCH_NUM);
|
|
|
|
return isset($data[0])?$data[0]:1;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* returns simple cached query
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function getQuery()
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
return $this->query;
|
|
|
|
}
|
|
|
|
/**
|
2006-07-26 21:17:59 +04:00
|
|
|
* returns internal data, used by Doctrine_Record instances
|
2006-05-30 12:42:10 +04:00
|
|
|
* when retrieving data from database
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
final public function getData()
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
return $this->data;
|
|
|
|
}
|
2007-02-08 02:10:41 +03:00
|
|
|
/**
|
|
|
|
* getter for associated tree
|
|
|
|
*
|
|
|
|
* @return mixed if tree return instance of Doctrine_Tree, otherwise returns false
|
|
|
|
*/
|
|
|
|
public function getTree() {
|
|
|
|
if (isset($this->options['treeImpl'])) {
|
|
|
|
if ( ! $this->tree) {
|
2007-02-08 16:39:27 +03:00
|
|
|
$options = isset($this->options['treeOptions']) ? $this->options['treeOptions'] : array();
|
2007-02-08 02:10:41 +03:00
|
|
|
$this->tree = Doctrine_Tree::factory($this,
|
2007-04-11 23:06:13 +04:00
|
|
|
$this->options['treeImpl'],
|
|
|
|
$options
|
|
|
|
);
|
2007-02-08 02:10:41 +03:00
|
|
|
}
|
|
|
|
return $this->tree;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2007-02-08 16:56:23 +03:00
|
|
|
public function getComponentName()
|
|
|
|
{
|
|
|
|
return $this->options['name'];
|
|
|
|
}
|
|
|
|
public function getTableName()
|
|
|
|
{
|
|
|
|
return $this->options['tableName'];
|
|
|
|
}
|
2007-02-09 01:23:47 +03:00
|
|
|
public function setTableName($tableName)
|
2007-02-09 01:19:17 +03:00
|
|
|
{
|
2007-02-09 01:23:47 +03:00
|
|
|
$this->options['tableName'] = $tableName;
|
2007-02-09 01:19:17 +03:00
|
|
|
}
|
2007-02-08 02:10:41 +03:00
|
|
|
/**
|
|
|
|
* determine if table acts as tree
|
|
|
|
*
|
2007-02-08 15:53:32 +03:00
|
|
|
* @return mixed if tree return true, otherwise returns false
|
2007-02-08 02:10:41 +03:00
|
|
|
*/
|
|
|
|
public function isTree() {
|
|
|
|
return ( ! is_null($this->options['treeImpl'])) ? true : false;
|
|
|
|
}
|
2006-05-30 12:42:10 +04:00
|
|
|
/**
|
|
|
|
* returns a string representation of this object
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function __toString()
|
|
|
|
{
|
2006-05-30 12:42:10 +04:00
|
|
|
return Doctrine_Lib::getTableAsString($this);
|
|
|
|
}
|
|
|
|
}
|