- Timestamp:
- 07/10/08 18:17:58 (6 months ago)
- Location:
- trunk
- Files:
-
- 17 modified
-
lib/Doctrine/ClassMetadata.php (modified) (33 diffs)
-
lib/Doctrine/ClassMetadata/CodeDriver.php (modified) (1 diff)
-
lib/Doctrine/ClassMetadata/Factory.php (modified) (6 diffs)
-
lib/Doctrine/Connection/UnitOfWork.php (modified) (5 diffs)
-
lib/Doctrine/Entity.php (modified) (34 diffs)
-
lib/Doctrine/EntityManager.php (modified) (15 diffs)
-
lib/Doctrine/EntityPersister/Abstract.php (modified) (4 diffs)
-
lib/Doctrine/EntityPersister/JoinedSubclass.php (modified) (4 diffs)
-
lib/Doctrine/EntityRepository.php (modified) (2 diffs)
-
lib/Doctrine/EventListener.php (modified) (1 diff)
-
lib/Doctrine/EventManager.php (modified) (1 diff)
-
lib/Doctrine/HydratorNew.php (modified) (5 diffs)
-
lib/Doctrine/Query/Production/IndexBy.php (modified) (1 diff)
-
tests/models/forum/ForumBoard.php (modified) (1 diff)
-
tests/models/forum/ForumCategory.php (modified) (1 diff)
-
tests/Orm/Entity/AccessorTestCase.php (modified) (2 diffs)
-
tests/Orm/Entity/ConstructorTest.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Doctrine/ClassMetadata.php
r4628 r4653 25 25 * A <tt>ClassMetadata</tt> instance holds all the information (metadata) of an entity and 26 26 * it's associations and how they're mapped to the relational database. 27 * It is the backbone of Doctrine's metadata mapping. 27 28 * 28 * @package Doctrine29 * @subpackage ClassMetadata30 29 * @author Roman Borschel <roman@code-factory.org> 31 30 * @since 2.0 … … 116 115 117 116 /** 118 * The mapped columns and their mapping definitions.119 * Keys are columnnames and values are mapping definitions.117 * The field mappings of the class. 118 * Keys are field names and values are mapping definitions. 120 119 * 121 120 * The mapping definition array has at least the following values: … … 129 128 * ... many more 130 129 * 131 * @var array $columns132 */ 133 protected $_ mappedColumns = array();130 * @var array 131 */ 132 protected $_fieldMappings = array(); 134 133 135 134 /** … … 283 282 */ 284 283 protected $_invokedMethods = array(); 285 284 285 /** 286 * The cached lifecycle listeners. There is only one instance of each 287 * listener class at any time. 288 * 289 * @var array 290 */ 291 protected $_lifecycleListenerInstances = array(); 292 293 /** 294 * The registered lifecycle callbacks for Entities of this class. 295 * 296 * @var array 297 */ 298 protected $_lifecycleCallbacks = array(); 299 300 /** 301 * The registered lifecycle listeners for Entities of this class. 302 * 303 * @var array 304 */ 305 protected $_lifecycleListeners = array(); 306 286 307 287 308 /** … … 375 396 public function isUniqueField($fieldName) 376 397 { 377 $mapping = $this->get ColumnMapping($fieldName);398 $mapping = $this->getFieldMapping($fieldName); 378 399 379 400 if ($mapping !== false) { … … 392 413 public function isNotNull($fieldName) 393 414 { 394 $mapping = $this->get ColumnMapping($fieldName);415 $mapping = $this->getFieldMapping($fieldName); 395 416 396 417 if ($mapping !== false) { … … 532 553 { 533 554 return isset($this->_columnNames[$fieldName]) ? 534 $this->_columnNames[$fieldName] : $fieldName;555 $this->_columnNames[$fieldName] : $fieldName; 535 556 } 536 557 … … 543 564 } 544 565 545 public function get ColumnMapping($columnName)546 { 547 return isset($this->_ mappedColumns[$columnName]) ?548 $this->_ mappedColumns[$columnName] : false;566 public function getFieldMapping($fieldName) 567 { 568 return isset($this->_fieldMappings[$fieldName]) ? 569 $this->_fieldMappings[$fieldName] : false; 549 570 } 550 571 … … 594 615 * This lookup on subclasses is costly but happens only *once* for a column 595 616 * during hydration because the hydrator caches effectively. 617 * 618 * @return string The field name. 619 * @throws Doctrine::ORM::Exceptions::ClassMetadataException if the field name could 620 * not be found. 596 621 */ 597 622 public function lookupFieldName($lcColumnName) … … 627 652 628 653 /** 629 * Maps a column of the class' database table to a field of the entity.654 * Maps a field of the class to a database column. 630 655 * 631 656 * @param string $name The name of the column to map. Syntax: columnName [as propertyName]. … … 640 665 * @throws Doctrine_ClassMetadata_Exception If trying use wrongly typed parameter. 641 666 */ 642 public function mapColumn($name, $type, $length = null, $options = array() , $prepend = false)667 public function mapColumn($name, $type, $length = null, $options = array()) 643 668 { 644 669 // converts 0 => 'primary' to 'primary' => true etc. … … 662 687 $lcColumnName = strtolower($parts[0]); 663 688 664 if (isset($this->_ mappedColumns[$columnName])) {689 if (isset($this->_fieldMappings[$fieldName])) { 665 690 return; 666 691 } 667 692 668 693 // Fill column name <-> field name lookup maps 669 if ($prepend) { 670 $this->_columnNames = array_merge(array($fieldName => $columnName), $this->_columnNames); 671 $this->_fieldNames = array_merge(array($columnName => $fieldName), $this->_fieldNames); 672 } else { 673 $this->_columnNames[$fieldName] = $columnName; 674 $this->_fieldNames[$columnName] = $fieldName; 675 } 694 $this->_columnNames[$fieldName] = $columnName; 695 $this->_fieldNames[$columnName] = $fieldName; 696 $this->_lcColumnToFieldNames[$lcColumnName] = $fieldName; 676 697 $this->_lcColumnToFieldNames[$lcColumnName] = $fieldName; 677 698 … … 702 723 }*/ 703 724 704 if ($prepend) { 705 $this->_mappedColumns = array_merge(array($columnName => $options), $this->_mappedColumns); 706 } else { 707 $this->_mappedColumns[$columnName] = $options; 708 } 725 $this->_fieldMappings[$fieldName] = $options; 709 726 710 727 $this->_columnCount++; … … 755 772 756 773 /** 757 * setColumn758 *759 * @param string $name760 * @param string $type761 * @param integer $length762 * @param mixed $options763 * @param boolean $prepend Whether to prepend or append the new column to the column list.764 * By default the column gets appended.765 * @throws Doctrine_Table_Exception if trying use wrongly typed parameter766 * @return void767 * @deprecated Use mapColumn()768 */769 public function setColumn($name, $type, $length = null, $options = array(), $prepend = false)770 {771 return $this->mapColumn($name, $type, $length, $options, $prepend);772 }773 774 /**775 774 * Gets the names of all validators that are applied on a field. 776 775 * … … 780 779 public function getFieldValidators($fieldName) 781 780 { 782 $columnName = $this->getColumnName($fieldName); 783 return isset($this->_mappedColumns[$columnName]['validators']) ? 784 $this->_mappedColumns[$columnName]['validators'] : array(); 781 return isset($this->_fieldMappings[$fieldName]['validators']) ? 782 $this->_fieldMappings[$fieldName]['validators'] : array(); 785 783 } 786 784 … … 804 802 public function getDefaultValueOf($fieldName) 805 803 { 806 $columnName = $this->getColumnName($fieldName); 807 if ( ! isset($this->_mappedColumns[$columnName])) { 808 throw new Doctrine_Table_Exception("Couldn't get default value. Column ".$columnName." doesn't exist."); 809 } 810 if (isset($this->_mappedColumns[$columnName]['default'])) { 811 return $this->_mappedColumns[$columnName]['default']; 804 if ( ! isset($this->_fieldMappings[$fieldName])) { 805 throw new Doctrine_Table_Exception("Couldn't get default value. Column ".$fieldName." doesn't exist."); 806 } 807 if (isset($this->_fieldMappings[$fieldName]['default'])) { 808 return $this->_fieldMappings[$fieldName]['default']; 812 809 } else { 813 810 return null; … … 868 865 public function hasColumn($columnName) 869 866 { 870 return isset($this->_ mappedColumns[$columnName]);867 return isset($this->_fieldNames[$columnName]); 871 868 } 872 869 873 870 public function hasMappedColumn($columnName) 874 871 { 875 return isset($this->_ mappedColumns[$columnName]);872 return isset($this->_fieldNames[$columnName]); 876 873 } 877 874 … … 891 888 public function getEnumValues($fieldName) 892 889 { 893 $columnName = $this->getColumnName($fieldName); 894 if (isset($this->_mappedColumns[$columnName]['values'])) { 895 return $this->_mappedColumns[$columnName]['values']; 890 if (isset($this->_fieldMappings[$fieldName]['values'])) { 891 return $this->_fieldMappings[$fieldName]['values']; 896 892 } else { 897 893 return array(); … … 918 914 $columnName = $this->getColumnName($fieldName); 919 915 if ( ! $this->_em->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM) && 920 isset($this->_mappedColumns[$columnName]['values'][$index])) {921 $enumValue = $this->_ mappedColumns[$columnName]['values'][$index];916 isset($this->_fieldMappings[$fieldName]['values'][$index])) { 917 $enumValue = $this->_fieldMappings[$fieldName]['values'][$index]; 922 918 } else { 923 919 $enumValue = $index; … … 974 970 public function getCustomAccessor($fieldName) 975 971 { 976 $columnName = $this->getColumnName($fieldName); 977 return isset($this->_mappedColumns[$columnName]['accessor']) ? 978 $this->_mappedColumns[$columnName]['accessor'] : null; 972 return isset($this->_fieldMappings[$fieldName]['accessor']) ? 973 $this->_fieldMappings[$fieldName]['accessor'] : null; 979 974 } 980 975 … … 986 981 public function getCustomMutator($fieldName) 987 982 { 988 $columnName = $this->getColumnName($fieldName); 989 return isset($this->_mappedColumns[$columnName]['mutator']) ? 990 $this->_mappedColumns[$columnName]['mutator'] : null; 983 return isset($this->_fieldMappings[$fieldName]['mutator']) ? 984 $this->_fieldMappings[$fieldName]['mutator'] : null; 991 985 } 992 986 … … 999 993 public function getColumns() 1000 994 { 1001 return $this->_ mappedColumns;995 return $this->_fieldMappings; 1002 996 } 1003 997 … … 1009 1003 public function getMappedColumns() 1010 1004 { 1011 return $this->_mappedColumns; 1005 return $this->_fieldMappings; 1006 } 1007 1008 public function getFieldMappings() 1009 { 1010 return $this->_fieldMappings; 1012 1011 } 1013 1012 … … 1024 1023 unset($this->_fieldNames[$columnName]); 1025 1024 1026 if (isset($this->_ mappedColumns[$columnName])) {1027 unset($this->_ mappedColumns[$columnName]);1025 if (isset($this->_fieldMappings[$fieldName])) { 1026 unset($this->_fieldMappings[$fieldName]); 1028 1027 return true; 1029 1028 } … … 1041 1040 { 1042 1041 if ($fieldNames === null) { 1043 return array_keys($this->_ mappedColumns);1042 return array_keys($this->_fieldNames); 1044 1043 } else { 1045 1044 $columnNames = array(); … … 1093 1092 public function getMappingForField($fieldName) 1094 1093 { 1095 $columnName = $this->getColumnName($fieldName); 1096 return $this->getColumnDefinition($columnName); 1094 return $this->_fieldMappings[$fieldName]; 1097 1095 } 1098 1096 … … 1105 1103 public function getTypeOf($fieldName) 1106 1104 { 1105 1107 1106 return $this->getTypeOfColumn($this->getColumnName($fieldName)); 1108 1107 } … … 1116 1115 public function getTypeOfField($fieldName) 1117 1116 { 1118 return $this->getTypeOfColumn($this->getColumnName($fieldName)); 1117 return isset($this->_fieldMappings[$fieldName]) ? 1118 $this->_fieldMappings[$fieldName]['type'] : false; 1119 1119 } 1120 1120 … … 1126 1126 public function getTypeOfColumn($columnName) 1127 1127 { 1128 return isset($this->_mappedColumns[$columnName]) ? $this->_mappedColumns[$columnName]['type'] : false;1128 return $this->getTypeOfField($this->getFieldName($columnName)); 1129 1129 } 1130 1130 … … 1134 1134 public function getFieldLength($fieldName) 1135 1135 { 1136 return $this->_ mappedColumns[$this->getColumnName($fieldName)]['length'];1136 return $this->_fieldMappings[$fieldName]['length']; 1137 1137 } 1138 1138 … … 1503 1503 $options['foreignKeys'] = array(); 1504 1504 if ($parseForeignKeys && $this->getAttribute(Doctrine::ATTR_EXPORT) 1505 & Doctrine::EXPORT_CONSTRAINTS) {1505 & Doctrine::EXPORT_CONSTRAINTS) { 1506 1506 $constraints = array(); 1507 1507 $emptyIntegrity = array('onUpdate' => null, 'onDelete' => null); … … 1684 1684 public function isInheritedField($fieldName) 1685 1685 { 1686 return isset($this->_ mappedColumns[$this->getColumnName($fieldName)]['inherited']);1686 return isset($this->_fieldMappings[$fieldName]['inherited']); 1687 1687 } 1688 1688 … … 1913 1913 1914 1914 } 1915 1916 /** 1917 * Dispatches the lifecycle event of the given Entity to the registered 1918 * lifecycle callbacks and lifecycle listeners. 1919 * 1920 * @param string $event The lifecycle event. 1921 * @param Entity $entity The Entity on which the event occured. 1922 */ 1923 public function invokeLifecycleCallbacks($lifecycleEvent, Doctrine_Entity $entity) 1924 { 1925 foreach ($this->getLifecycleCallbacks($lifecycleEvent) as $callback) { 1926 $entity->$callback(); 1927 } 1928 foreach ($this->getLifecycleListeners($lifecycleEvent) as $className => $callback) { 1929 if ( ! isset($this->_lifecycleListenerInstances[$className])) { 1930 $this->_lifecycleListenerInstances[$className] = new $className; 1931 } 1932 $this->_lifecycleListenerInstances[$className]->$callback($entity); 1933 } 1934 } 1935 1936 /** 1937 * Gets the registered lifecycle callbacks for an event. 1938 * 1939 * @param string $event 1940 * @return array 1941 */ 1942 public function getLifecycleCallbacks($event) 1943 { 1944 return isset($this->_lifecycleCallbacks[$event]) ? 1945 $this->_lifecycleCallbacks[$event] : array(); 1946 } 1947 1948 /** 1949 * Gets the registered lifecycle listeners for an event. 1950 * 1951 * @param string $event 1952 * @return array 1953 */ 1954 public function getLifecycleListeners($event) 1955 { 1956 return isset($this->_lifecycleListeners[$event]) ? 1957 $this->_lifecycleListeners[$event] : array(); 1958 } 1959 1960 /** 1961 * Adds a lifecycle listener for Entities this class. 1962 * 1963 * Note: If the same listener class is registered more than once, the old 1964 * one will be overridden. 1965 * 1966 * @param string $listenerClass 1967 * @param array $callbacks 1968 */ 1969 public function addLifecycleListener($listenerClass, array $callbacks) 1970 { 1971 $this->_lifecycleListeners[$event][$listenerClass] = array(); 1972 foreach ($callbacks as $method => $event) { 1973 $this->_lifecycleListeners[$event][$listenerClass][] = $method; 1974 } 1975 } 1976 1977 /** 1978 * Adds a lifecycle callback for Entities of this class. 1979 * 1980 * Note: If a the same callback is registered more than once, the old one 1981 * will be overridden. 1982 * 1983 * @param string $callback 1984 * @param string $event 1985 */ 1986 public function addLifecycleCallback($callback, $event) 1987 { 1988 if ( ! isset($this->_lifecycleCallbacks[$event])) { 1989 $this->_lifecycleCallbacks[$event] = array(); 1990 } 1991 if ( ! in_array($callback, $this->_lifecycleCallbacks[$event])) { 1992 $this->_lifecycleCallbacks[$event][$callback] = $callback; 1993 } 1994 } 1915 1995 1916 1996 /** -
trunk/lib/Doctrine/ClassMetadata/CodeDriver.php
r4435 r4653 36 36 { 37 37 /** 38 * Name of the callback method. 39 * 40 * @todo We could make the name of the callback methods customizable for users. 41 */ 42 const CALLBACK_METHOD = 'initMetadata'; 43 44 /** 38 45 * Loads the metadata for the specified class into the provided container. 39 46 */ 40 47 public function loadMetadataForClass($className, Doctrine_ClassMetadata $metadata) 41 48 { 42 if ( ! method_exists($className, 'initMetadata')) {49 if ( ! method_exists($className, self::CALLBACK_METHOD)) { 43 50 throw new Doctrine_ClassMetadata_Exception("Unable to load metadata for class" 44 51 . " '$className'. Callback method 'initMetadata' not found."); -
trunk/lib/Doctrine/ClassMetadata/Factory.php
r4434 r4653 134 134 $fullName = "$name as " . $parentClass->getFieldName($name); 135 135 $definition['inherited'] = true; 136 $subClass->mapColumn($fullName, $definition['type'], $definition['length'], 136 $subClass->mapColumn( 137 $fullName, 138 $definition['type'], 139 $definition['length'], 137 140 $definition); 138 141 } … … 155 158 { 156 159 if ( ! class_exists($name) || empty($name)) { 157 /*try {158 throw new Exception();159 } catch (Exception $e) {160 echo $e->getTraceAsString();161 }*/162 160 throw new Doctrine_Exception("Couldn't find class " . $name . "."); 163 161 } … … 176 174 177 175 if ($className === false) { 178 try {179 throw new Exception();180 } catch (Exception $e) {181 echo $e->getTraceAsString() . "<br />";182 }183 176 throw new Doctrine_ClassMetadata_Factory_Exception("Unknown component '$className'."); 184 177 } … … 208 201 { 209 202 switch (count((array)$class->getIdentifier())) { 210 case 0: 203 case 0: // No identifier in the class mapping yet 204 205 // If its a subclass, inherit the identifier from the parent. 211 206 if ($class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_JOINED && 212 count($class->getParentClasses()) > 0) { 213 207 count($class->getParentClasses()) > 0) { 214 208 $parents = $class->getParentClasses(); 215 209 $root = end($parents); … … 238 232 } 239 233 } else { 234 throw Doctrine_MappingException::identifierRequired($class->getClassName()); 235 /* Legacy behavior of auto-adding an id field 240 236 $definition = array('type' => 'integer', 241 237 'length' => 20, 242 238 'autoincrement' => true, 243 239 'primary' => true); 244 $class-> setColumn('id', $definition['type'], $definition['length'], $definition, true);240 $class->mapColumn('id', $definition['type'], $definition['length'], $definition, true); 245 241 $class->setIdentifier(array('id')); 246 242 $class->setIdentifierType(Doctrine::IDENTIFIER_AUTOINC); 243 */ 247 244 } 248 245 break; 249 case 1: 250 foreach ( (array)$class->getIdentifier() as $pk) {246 case 1: // A single identifier is in the mapping 247 foreach ($class->getIdentifier() as $pk) { 251 248 $columnName = $class->getColumnName($pk); 252 249 $thisColumns = $class->getColumns(); … … 295 292 296 293 break; 297 default: 294 default: // Multiple identifiers are in the mapping so its a composite id 298 295 $class->setIdentifierType(Doctrine::IDENTIFIER_COMPOSITE); 299 296 } -
trunk/lib/Doctrine/Connection/UnitOfWork.php
r4628 r4653 84 84 85 85 /** 86 * The EntityManager the unit of work belongs to.86 * The EntityManager the UnitOfWork belongs to. 87 87 */ 88 88 protected $_em; … … 99 99 /** 100 100 * Constructor. 101 * Create da new UnitOfWork.101 * Creates a new UnitOfWork. 102 102 * 103 103 * @param Doctrine_EntityManager $em … … 111 111 * Commits the unit of work, executing all operations that have been postponed 112 112 * up to this point. 113 * 113 * 114 * @todo Impl 114 115 */ 115 116 public function commit() … … 192 193 public function registerRemoved(Doctrine_Entity $entity) 193 194 { 194 if ($entity->is Transient()) {195 if ($entity->isNew()) { 195 196 return; 196 197 } … … 512 513 } 513 514 515 public function save(Doctrine_Entity $entity) 516 { 517 switch ($entity->_state()) { 518 case Doctrine_Entity::STATE_CLEAN: 519 //nothing to do 520 // ignore $entity but cascade 521 break; 522 case Doctrine_Entity::STATE_DIRTY: 523 // update 524 $this->registerDirty($entity); 525 // todo:cascade 526 break; 527 case Doctrine_Entity::STATE_TCLEAN: 528 case Doctrine_Entity::STATE_TDIRTY: 529 // insert 530 // if identifier type IDENTITY: 531 // cascade 532 // &nb