1
0
mirror of synced 2024-12-13 22:56:04 +03:00
doctrine2/lib/Doctrine/Table.php

1139 lines
37 KiB
PHP
Raw Normal View History

2006-05-30 12:42:10 +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>.
*/
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-05-30 12:42:10 +04:00
*
* @author Konsta Vesterinen
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
* @version 1.0 alpha
*/
class Doctrine_Table extends Doctrine_Configurable implements Countable {
2006-05-30 12:42:10 +04:00
/**
* @var boolean $isNewEntry whether ot not this table created a new record or not, used only internally
*/
private $isNewEntry = false;
/**
* @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;
/**
* @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-09-03 23:20:02 +04:00
* @var Doctrine_Connection $connection Doctrine_Connection object that created this table
2006-05-30 12:42:10 +04:00
*/
2006-08-22 03:21:01 +04:00
private $connection;
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
/**
* @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
*
* the value array has three values:
*
* 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(
* 'name' => array('string', 20, array('notnull' => true, 'default' => 'someone')
* 'age' => array('integer', 11, array('notnull' => true))
* )
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
/**
* @var array $bound bound relations
*/
private $bound = array();
/**
* @var array $boundAliases bound relation aliases
*/
private $boundAliases = array();
/**
* @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;
/**
* @var array $parents the parent classes of this component
*/
private $parents = array();
/**
* @var boolean $hasDefaultValues whether or not this table has default values
*/
private $hasDefaultValues;
2006-10-24 21:02:47 +04:00
/**
* @var array $options an array containing all options
*
* -- name name of the component, for example component name of the GroupTable is 'Group'
*
* -- 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
*/
protected $options = array('name' => null,
'tableName' => null,
'sequenceName' => null,
'inheritanceMap' => array(),
'enumMap' => array(),
);
2006-06-18 02:46:03 +04:00
2006-05-30 12:42:10 +04:00
2006-09-03 23:20:02 +04:00
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
*/
public function __construct($name, Doctrine_Connection $conn) {
$this->connection = $conn;
2006-05-30 12:42:10 +04:00
2006-08-22 03:21:01 +04:00
$this->setParent($this->connection);
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
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 {
2006-09-18 23:52:17 +04:00
if($class == "Doctrine_Record")
break;
2006-05-30 12:42:10 +04:00
$name = $class;
$names[] = $name;
} while($class = get_parent_class($class));
// reverse names
$names = array_reverse($names);
// create database table
if(method_exists($record, 'setTableDefinition')) {
2006-05-30 12:42:10 +04:00
$record->setTableDefinition();
$this->columnCount = count($this->columns);
if(isset($this->columns)) {
2006-09-18 23:52:17 +04:00
// get the declaring class of setTableDefinition method
$method = new ReflectionMethod($this->options['name'], 'setTableDefinition');
2006-05-30 12:42:10 +04:00
$class = $method->getDeclaringClass();
2006-10-24 21:02:47 +04:00
if( ! isset($this->options['tableName']))
$this->options['tableName'] = Doctrine::tableize($class->getName());
2006-05-30 12:42:10 +04:00
switch(count($this->primaryKeys)):
case 0:
$this->columns = array_merge(array('id' =>
array('integer',
20,
array('autoincrement' => true,
'primary' => true
)
)
), $this->columns);
$this->primaryKeys[] = 'id';
$this->identifier = 'id';
2006-05-30 12:42:10 +04:00
$this->identifierType = Doctrine_Identifier::AUTO_INCREMENT;
$this->columnCount++;
2006-05-30 12:42:10 +04:00
break;
default:
if(count($this->primaryKeys) > 1) {
$this->identifier = $this->primaryKeys;
$this->identifierType = Doctrine_Identifier::COMPOSITE;
2006-05-30 12:42:10 +04:00
} else {
foreach($this->primaryKeys as $pk) {
$e = $this->columns[$pk][2];
$found = false;
2006-05-30 12:42:10 +04:00
foreach($e as $option => $value) {
2006-05-30 12:42:10 +04:00
if($found)
break;
2006-05-30 12:42:10 +04:00
$e2 = explode(":",$option);
2006-05-30 12:42:10 +04:00
switch(strtolower($e2[0])):
case "autoincrement":
$this->identifierType = Doctrine_Identifier::AUTO_INCREMENT;
$found = true;
break;
case "seq":
$this->identifierType = Doctrine_Identifier::SEQUENCE;
$found = true;
break;
endswitch;
}
if( ! isset($this->identifierType))
$this->identifierType = Doctrine_Identifier::NORMAL;
2006-05-30 12:42:10 +04:00
$this->identifier = $pk;
}
}
endswitch;
2006-08-29 23:34:03 +04:00
if($this->getAttribute(Doctrine::ATTR_CREATE_TABLES)) {
if(Doctrine_DataDict::isValidClassname($class->getName())) {
$dict = new Doctrine_DataDict($this->getConnection()->getDBH());
2006-10-24 21:02:47 +04:00
$dict->createTable($this->options['tableName'], $this->columns);
2006-08-29 23:34:03 +04: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-09-03 23:20:02 +04:00
2006-05-30 12:42:10 +04:00
$record->setUp();
// save parents
array_pop($names);
$this->parents = $names;
$this->query = "SELECT ".implode(", ",array_keys($this->columns))." FROM ".$this->getTableName();
// check if an instance of this table is already initialized
2006-08-22 03:21:01 +04:00
if( ! $this->connection->addTable($this))
2006-05-30 12:42:10 +04:00
throw new Doctrine_Table_Exception();
$this->repository = new Doctrine_Table_Repository($this);
2006-05-30 12:42:10 +04:00
}
/**
* getRepository
*
* @return Doctrine_Table_Repository
2006-05-30 12:42:10 +04:00
*/
public function getRepository() {
return $this->repository;
}
2006-10-24 21:02:47 +04:00
public function setOption($name, $value) {
switch($name) {
case 'name':
case 'tableName':
break;
case 'enumMap':
case 'inheritanceMap':
if( ! is_array($value))
throw new Doctrine_Table_Exception($name.' should be an array.');
break;
}
$this->options[$name] = $value;
}
2006-05-30 12:42:10 +04:00
/**
* setColumn
* @param string $name
* @param string $type
* @param integer $length
* @param mixed $options
* @return void
*/
final public function setColumn($name, $type, $length, $options = array()) {
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]);
}
}
$name = strtolower($name);
2006-05-30 12:42:10 +04:00
$this->columns[$name] = array($type,$length,$options);
if(isset($options['primary'])) {
2006-05-30 12:42:10 +04:00
$this->primaryKeys[] = $name;
}
if(isset($options['default'])) {
$this->hasDefaultValues = true;
}
}
/**
* hasDefaultValues
* returns true if this table has default values, otherwise false
*
* @return boolean
*/
public function hasDefaultValues() {
return $this->hasDefaultValues;
2006-05-30 12:42:10 +04: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.");
2006-09-03 23:20:02 +04:00
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
*/
final public function getIdentifier() {
return $this->identifier;
}
/**
* @return integer
*/
final public function getIdentifierType() {
return $this->identifierType;
}
/**
* hasColumn
* @return boolean
*/
final public function hasColumn($name) {
return isset($this->columns[$name]);
}
/**
* @param mixed $key
* @return void
*/
final public function setPrimaryKey($key) {
switch(gettype($key)):
case "array":
$this->primaryKeys = array_values($key);
break;
case "string":
$this->primaryKeys[] = $key;
break;
endswitch;
}
/**
* returns all primary keys
* @return array
*/
final public function getPrimaryKeys() {
return $this->primaryKeys;
}
/**
* @return boolean
*/
final public function hasPrimaryKey($key) {
return in_array($key,$this->primaryKeys);
}
/**
* @param $sequence
* @return void
*/
final public function setSequenceName($sequence) {
2006-10-24 21:02:47 +04:00
$this->options['sequenceName'] = $sequence;
2006-05-30 12:42:10 +04:00
}
/**
* @return string sequence name
*/
final public function getSequenceName() {
2006-10-24 21:02:47 +04:00
return $this->options['sequenceName'];
2006-05-30 12:42:10 +04:00
}
/**
* getParents
*/
final public function getParents() {
2006-06-18 02:46:03 +04:00
return $this->parents;
}
/**
* @return boolean
*/
final public function hasInheritanceMap() {
2006-10-24 21:02:47 +04:00
return (empty($this->options['inheritanceMap']));
2006-05-30 12:42:10 +04:00
}
/**
* @return array inheritance map (array keys as fields)
*/
final public function getInheritanceMap() {
2006-10-24 21:02:47 +04:00
return $this->options['inheritanceMap'];
2006-05-30 12:42:10 +04:00
}
/**
* return all composite paths in the form [component1].[component2]. . .[componentN]
* @return array
*/
final public function getCompositePaths() {
$array = array();
$name = $this->getComponentName();
foreach($this->bound as $k=>$a) {
try {
$fk = $this->getRelation($k);
2006-05-30 12:42:10 +04:00
switch($fk->getType()):
case Doctrine_Relation::ONE_COMPOSITE:
case Doctrine_Relation::MANY_COMPOSITE:
$n = $fk->getTable()->getComponentName();
$array[] = $name.".".$n;
$e = $fk->getTable()->getCompositePaths();
if( ! empty($e)) {
foreach($e as $name) {
$array[] = $name.".".$n.".".$name;
}
}
break;
endswitch;
} catch(Doctrine_Table_Exception $e) {
2006-05-30 12:42:10 +04:00
}
}
return $array;
}
/**
* returns all bound relations
*
* @return array
*/
public function getBounds() {
2006-05-30 12:42:10 +04:00
return $this->bound;
}
/**
* returns a bound relation array
*
* @param string $name
* @return array
*/
public function getBound($name) {
if( ! isset($this->bound[$name]))
throw new Doctrine_Table_Exception('Unknown bound '.$name);
2006-05-30 12:42:10 +04:00
return $this->bound[$name];
}
/**
* returns a bound relation array
*
* @param string $name
* @return array
*/
public function getBoundForName($name, $component) {
2006-05-30 12:42:10 +04:00
foreach($this->bound as $k => $bound) {
$e = explode('.', $bound[0]);
if($bound[3] == $name && $e[0] == $component) {
2006-05-30 12:42:10 +04:00
return $this->bound[$k];
}
}
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
*/
final public function getAlias($name) {
if(isset($this->boundAliases[$name]))
return $this->boundAliases[$name];
2006-05-30 12:42:10 +04:00
return $name;
}
/**
* returns component name for given alias
*
2006-05-30 12:42:10 +04:00
* @param string $alias
* @return string
*/
final public function getAliasName($alias) {
if($name = array_search($this->boundAliases,$alias))
return $name;
throw new Doctrine_Table_Exception('Unknown alias '.$alias);
2006-05-30 12:42:10 +04:00
}
/**
* unbinds all relations
*
2006-05-30 12:42:10 +04:00
* @return void
*/
public function unbindAll() {
2006-05-30 12:42:10 +04:00
$this->bound = array();
$this->relations = array();
$this->boundAliases = array();
}
/**
* unbinds a relation
* returns true on success, false on failure
*
* @param $name
* @return boolean
*/
public function unbind($name) {
2006-05-30 12:42:10 +04:00
if( ! isset($this->bound[$name]))
return false;
2006-05-30 12:42:10 +04:00
unset($this->bound[$name]);
if(isset($this->relations[$name]))
unset($this->relations[$name]);
if(isset($this->boundAliases[$name]))
unset($this->boundAliases[$name]);
return true;
}
/**
* binds a relation
*
* @param string $name
* @param string $field
* @return void
*/
final public function bind($name, $field, $type, $localKey) {
2006-05-30 12:42:10 +04:00
if(isset($this->relations[$name]))
unset($this->relations[$name]);
2006-05-30 12:42:10 +04:00
$e = explode(" as ",$name);
$name = $e[0];
if(isset($e[1])) {
$alias = $e[1];
$this->boundAliases[$name] = $alias;
} else
$alias = $name;
$this->bound[$alias] = array($field,$type,$localKey,$name);
}
/**
* getComponentName
* @return string the component name
*/
public function getComponentName() {
2006-10-24 21:02:47 +04:00
return $this->options['name'];
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
*/
public function getConnection() {
2006-08-22 03:21:01 +04:00
return $this->connection;
2006-05-30 12:42:10 +04:00
}
/**
* hasRelatedComponent
* @return boolean
*/
final public function hasRelatedComponent($name, $component) {
return (strpos($this->bound[$name][0], $component.'.') !== false);
}
/**
* @param string $name component name of which a foreign key object is bound
* @return boolean
*/
final public function hasRelation($name) {
if(isset($this->bound[$name]))
return true;
foreach($this->bound as $k=>$v)
{
if($this->hasRelatedComponent($k, $name))
return true;
}
return false;
}
2006-05-30 12:42:10 +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
*/
final public function getRelation($name, $recursive = true) {
2006-08-01 22:02:53 +04:00
$original = $name;
2006-05-30 12:42:10 +04:00
if(isset($this->relations[$name]))
2006-10-20 22:21:42 +04:00
return $this->relations[$name];
2006-05-30 12:42:10 +04:00
if(isset($this->bound[$name])) {
$type = $this->bound[$name][1];
$local = $this->bound[$name][2];
list($component, $foreign) = explode(".",$this->bound[$name][0]);
$alias = $name;
$name = $this->bound[$alias][3];
2006-08-22 03:21:01 +04:00
$table = $this->connection->getTable($name);
2006-05-30 12:42:10 +04:00
2006-10-24 21:02:47 +04:00
if($component == $this->options['name'] || in_array($component, $this->parents)) {
2006-05-30 12:42:10 +04:00
// ONE-TO-ONE
if($type == Doctrine_Relation::ONE_COMPOSITE ||
$type == Doctrine_Relation::ONE_AGGREGATE) {
if( ! isset($local))
$local = $table->getIdentifier();
2006-10-23 20:19:47 +04:00
$relation = new Doctrine_Relation_LocalKey($table, $foreign, $local, $type, $alias);
2006-05-30 12:42:10 +04:00
} else
2006-10-23 20:19:47 +04:00
$relation = new Doctrine_Relation_ForeignKey($table, $foreign, $local, $type, $alias);
2006-05-30 12:42:10 +04:00
2006-10-20 22:21:42 +04:00
} elseif($component == $name ||
2006-10-24 21:02:47 +04:00
($component == $alias)) { // && ($name == $this->options['name'] || in_array($name,$this->parents))
2006-09-19 00:11:21 +04:00
2006-05-30 12:42:10 +04:00
if( ! isset($local))
$local = $this->identifier;
// ONE-TO-MANY or ONE-TO-ONE
$relation = new Doctrine_Relation_ForeignKey($table, $local, $foreign, $type, $alias);
2006-05-30 12:42:10 +04:00
} else {
// MANY-TO-MANY
// only aggregate relations allowed
if($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
2006-10-24 21:02:47 +04:00
$classes = array_merge($this->parents, array($this->options['name']));
2006-05-30 12:42:10 +04:00
foreach(array_reverse($classes) as $class) {
try {
$bound = $table->getBoundForName($class, $component);
2006-05-30 12:42:10 +04:00
break;
} catch(Doctrine_Table_Exception $exc) { }
2006-05-30 12:42:10 +04:00
}
if( ! isset($local))
$local = $this->identifier;
$e2 = explode('.', $bound[0]);
$fields = explode('-', $e2[1]);
2006-05-30 12:42:10 +04:00
if($e2[0] != $component)
throw new Doctrine_Table_Exception($e2[0] . ' doesn\'t match ' . $component);
2006-05-30 12:42:10 +04:00
2006-08-22 03:21:01 +04:00
$associationTable = $this->connection->getTable($e2[0]);
2006-05-30 12:42:10 +04:00
if(count($fields) > 1) {
// SELF-REFERENCING THROUGH JOIN TABLE
$this->relations[$e2[0]] = new Doctrine_Relation_ForeignKey($associationTable,$local,$fields[0],Doctrine_Relation::MANY_COMPOSITE, $e2[0]);
$relation = new Doctrine_Relation_Association_Self($table,$associationTable,$fields[0],$fields[1], $type, $alias);
2006-05-30 12:42:10 +04:00
} else {
// auto initialize a new one-to-one relationship for association table
2006-09-18 23:52:17 +04:00
$associationTable->bind($this->getComponentName(), $associationTable->getComponentName(). '.' .$e2[1], Doctrine_Relation::ONE_AGGREGATE, 'id');
$associationTable->bind($table->getComponentName(), $associationTable->getComponentName(). '.' .$foreign, Doctrine_Relation::ONE_AGGREGATE, 'id');
2006-05-30 12:42:10 +04:00
// NORMAL MANY-TO-MANY RELATIONSHIP
$this->relations[$e2[0]] = new Doctrine_Relation_ForeignKey($associationTable,$local,$e2[1],Doctrine_Relation::MANY_COMPOSITE, $e2[0]);
2006-05-30 12:42:10 +04:00
$relation = new Doctrine_Relation_Association($table, $associationTable, $e2[1], $foreign, $type, $alias);
2006-05-30 12:42:10 +04:00
}
}
2006-10-20 22:21:42 +04:00
2006-05-30 12:42:10 +04:00
$this->relations[$alias] = $relation;
return $this->relations[$alias];
}
2006-10-20 22:21:42 +04:00
// load all relations
$this->getRelations();
if($recursive) {
return $this->getRelation($original, false);
} else {
2006-10-24 21:02:47 +04:00
throw new Doctrine_Table_Exception($this->options['name'] . " doesn't have a relation to " . $original);
}
2006-05-30 12:42:10 +04:00
}
/**
* returns an array containing all foreign key objects
*
* @return array
*/
final public function getRelations() {
2006-05-30 12:42:10 +04:00
$a = array();
foreach($this->bound as $k=>$v) {
$this->getRelation($k);
2006-05-30 12:42:10 +04:00
}
return $this->relations;
}
/**
* sets the database table name
*
* @param string $name database table name
* @return void
*/
final public function setTableName($name) {
2006-10-24 21:02:47 +04:00
$this->options['tableName'] = $name;
2006-05-30 12:42:10 +04:00
}
/**
* returns the database table name
*
* @return string
*/
final public function getTableName() {
2006-10-24 21:02:47 +04:00
return $this->options['tableName'];
2006-05-30 12:42:10 +04:00
}
/**
* 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()) {
$this->data = $array;
2006-05-30 12:42:10 +04:00
$this->isNewEntry = true;
2006-10-24 21:02:47 +04:00
$record = new $this->options['name']($this);
2006-05-30 12:42:10 +04:00
$this->isNewEntry = false;
$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
*/
public function find($id) {
if($id !== null) {
if( ! is_array($id))
$id = array($id);
else
2006-05-30 12:42:10 +04:00
$id = array_values($id);
$query = $this->query." WHERE ".implode(" = ? AND ",$this->primaryKeys)." = ?";
$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
2006-08-22 03:21:01 +04:00
$stmt = $this->connection->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
if($this->data === false)
return false;
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
*/
final public function applyInheritance($where) {
2006-10-24 21:02:47 +04:00
if( ! empty($this->options['inheritanceMap'])) {
2006-05-30 12:42:10 +04:00
$a = array();
2006-10-24 21:02:47 +04:00
foreach($this->options['inheritanceMap'] as $field => $value) {
2006-05-30 12:42:10 +04:00
$a[] = $field." = ?";
}
$i = implode(" AND ",$a);
$where .= " AND $i";
}
return $where;
}
/**
* findAll
* returns a collection of records
*
* @return Doctrine_Collection
*/
public function findAll() {
2006-08-22 03:21:01 +04:00
$graph = new Doctrine_Query($this->connection);
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;
}
/**
* findByDql
* finds records with given DQL where clause
2006-05-30 12:42:10 +04:00
* returns a collection of records
*
* @param string $dql DQL after WHERE clause
2006-05-30 12:42:10 +04:00
* @param array $params query parameters
* @return Doctrine_Collection
*/
public function findBySql($dql, array $params = array()) {
2006-08-22 03:21:01 +04:00
$q = new Doctrine_Query($this->connection);
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-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
*/
public function clear() {
$this->identityMap = array();
}
/**
* getRecord
* first checks if record exists in identityMap, if not
* returns a new record
*
* @return Doctrine_Record
*/
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();
if( ! is_array($key))
$key = array($key);
foreach($key as $k) {
if( ! isset($this->data[$k]))
2006-08-22 23:34:40 +04:00
throw new Doctrine_Exception("Primary key value for $k wasn't found");
2006-06-01 15:58:05 +04: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);
if(isset($this->identityMap[$id]))
$record = $this->identityMap[$id];
else {
2006-10-24 21:02:47 +04:00
$record = new $this->options['name']($this);
2006-05-30 12:42:10 +04:00
$this->identityMap[$id] = $record;
}
$this->data = array();
return $record;
}
/**
* @param $id database row id
* @throws Doctrine_Find_Exception
* @return DAOProxy a proxy for given identifier
*/
final public function getProxy($id = null) {
if($id !== null) {
$query = "SELECT ".implode(", ",$this->primaryKeys)." FROM ".$this->getTableName()." WHERE ".implode(" = ? && ",$this->primaryKeys)." = ?";
$query = $this->applyInheritance($query);
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-08-22 03:21:01 +04:00
$this->data = $this->connection->execute($query,$params)->fetch(PDO::FETCH_ASSOC);
2006-05-30 12:42:10 +04:00
if($this->data === false)
return false;
2006-05-30 12:42:10 +04:00
}
return $this->getRecord();
}
/**
* getTableDescription
2006-09-24 13:52:04 +04:00
* @return array
2006-05-30 12:42:10 +04:00
*/
final public function getTableDescription() {
return $this->columns;
}
/**
* count
*
* @return integer
*/
public function count() {
2006-10-24 21:02:47 +04:00
$a = $this->connection->getDBH()->query("SELECT COUNT(1) FROM ".$this->options['tableName'])->fetch(PDO::FETCH_NUM);
return current($a);
}
2006-05-30 12:42:10 +04:00
/**
* @return Doctrine_Query a Doctrine_Query object
*/
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-08-22 03:21:01 +04:00
$query = $this->connection->modifyLimitQuery($query,$limit,$offset);
2006-05-30 12:42:10 +04:00
if( ! empty($array)) {
2006-08-22 03:21:01 +04:00
$stmt = $this->connection->getDBH()->prepare($query);
2006-05-30 12:42:10 +04:00
$stmt->execute($array);
} else {
2006-08-22 03:21:01 +04:00
$stmt = $this->connection->getDBH()->query($query);
2006-05-30 12:42:10 +04:00
}
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
foreach($data as $row) {
$this->data = $row;
$record = $this->getRecord();
$coll->add($record);
}
return $coll;
}
2006-06-18 02:46:03 +04:00
/**
* sets enumerated value array for given field
*
* @param string $field
* @param array $values
* @return void
*/
final public function setEnumValues($field, array $values) {
2006-10-24 21:02:47 +04:00
$this->options['enumMap'][strtolower($field)] = $values;
2006-06-18 02:46:03 +04:00
}
2006-06-20 01:31:22 +04:00
/**
* @param string $field
* @return array
*/
final public function getEnumValues($field) {
2006-10-24 21:02:47 +04:00
if(isset($this->options['enumMap'][$field]))
return $this->options['enumMap'][$field];
2006-06-20 01:31:22 +04:00
else
return array();
}
2006-06-18 02:46:03 +04:00
/**
* enumValue
*
* @param string $field
* @param integer $index
* @return mixed
2006-06-18 02:46:03 +04:00
*/
final public function enumValue($field, $index) {
if ($index instanceof Doctrine_Null)
return $index;
2006-10-24 21:02:47 +04:00
return isset($this->options['enumMap'][$field][$index]) ? $this->options['enumMap'][$field][$index] : $index;
2006-06-18 02:46:03 +04:00
}
/**
* invokeSet
*
* @param mixed $value
*/
public function invokeSet(Doctrine_Record $record, $name, $value) {
if( ! ($this->getAttribute(Doctrine::ATTR_ACCESSORS) | Doctrine::ACCESSOR_SET))
return $value;
$method = 'set' . $name;
if(method_exists($record, $method)) {
return $record->$method($value);
}
return $value;
}
/**
* invokeGet
*
* @param mixed $value
*/
public function invokeGet(Doctrine_Record $record, $name, $value) {
if( ! ($this->getAttribute(Doctrine::ATTR_ACCESSORS) | Doctrine::ACCESSOR_GET))
return $value;
$method = 'get' . $name;
if(method_exists($record, $method)) {
return $record->$method($value);
}
return $value;
}
2006-06-18 02:46:03 +04:00
/**
* enumIndex
*
* @param string $field
* @param mixed $value
* @return mixed
2006-06-18 02:46:03 +04:00
*/
final public function enumIndex($field, $value) {
2006-10-24 21:02:47 +04:00
if( ! isset($this->options['enumMap'][$field]))
$values = array();
else
2006-10-24 21:02:47 +04:00
$values = $this->options['enumMap'][$field];
return array_search($value, $values);
2006-06-18 02:46:03 +04:00
}
2006-05-30 12:42:10 +04:00
/**
* getDefinitionOf
*
* @return string ValueWrapper class name on success, false on failure
*/
public function getValueWrapperOf($column) {
if(isset($this->columns[$column][2]['wrapper']))
return $this->columns[$column][2]['wrapper'];
return false;
}
/**
* getColumnCount
*
* @return integer the number of columns in this table
2006-05-30 12:42:10 +04:00
*/
final public function getColumnCount() {
return $this->columnCount;
2006-05-30 12:42:10 +04:00
}
2006-06-18 02:46:03 +04:00
2006-05-30 12:42:10 +04:00
/**
* returns all columns and their definitions
*
* @return array
*/
final public function getColumns() {
return $this->columns;
}
/**
* returns an array containing all the column names
*
* @return array
*/
public function getColumnNames() {
return array_keys($this->columns);
}
2006-06-18 02:46:03 +04:00
/**
* getDefinitionOf
2006-06-25 22:34:53 +04:00
*
* @return mixed array on success, false on failure
2006-06-18 02:46:03 +04:00
*/
public function getDefinitionOf($column) {
if(isset($this->columns[$column]))
return $this->columns[$column];
2006-06-25 22:34:53 +04:00
return false;
2006-06-18 02:46:03 +04:00
}
2006-06-03 13:10:43 +04:00
/**
* getTypeOf
2006-06-25 22:34:53 +04:00
*
* @return mixed string on success, false on failure
2006-06-03 13:10:43 +04:00
*/
public function getTypeOf($column) {
if(isset($this->columns[$column]))
return $this->columns[$column][0];
2006-06-25 22:34:53 +04:00
return false;
2006-06-03 13:10:43 +04:00
}
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
*/
public function setData(array $data) {
$this->data = $data;
}
/**
* returns the maximum primary key value
*
* @return integer
*/
final public function getMaxIdentifier() {
$sql = "SELECT MAX(".$this->getIdentifier().") FROM ".$this->getTableName();
2006-08-22 03:21:01 +04:00
$stmt = $this->connection->getDBH()->query($sql);
2006-05-30 12:42:10 +04:00
$data = $stmt->fetch(PDO::FETCH_NUM);
return isset($data[0])?$data[0]:1;
}
/**
* return whether or not a newly created object is new or not
*
* @return boolean
*/
final public function isNewEntry() {
return $this->isNewEntry;
}
/**
* returns simple cached query
*
* @return string
*/
final public function getQuery() {
return $this->query;
}
/**
* returns internal data, used by Doctrine_Record instances
2006-05-30 12:42:10 +04:00
* when retrieving data from database
*
* @return array
*/
final public function getData() {
return $this->data;
}
/**
* returns a string representation of this object
*
* @return string
*/
public function __toString() {
return Doctrine_Lib::getTableAsString($this);
}
}