Changeset 4628 for trunk/lib/Doctrine
- Timestamp:
- 07/04/08 17:32:19 (6 months ago)
- Location:
- trunk/lib/Doctrine
- Files:
-
- 22 modified
-
ClassMetadata.php (modified) (9 diffs)
-
Collection.php (modified) (17 diffs)
-
Configuration.php (modified) (1 diff)
-
Connection.php (modified) (23 diffs)
-
Connection/Mock.php (modified) (1 diff)
-
Connection/Mysql.php (modified) (2 diffs)
-
Connection/Sqlite.php (modified) (5 diffs)
-
Connection/UnitOfWork.php (modified) (8 diffs)
-
ConnectionFactory.php (modified) (2 diffs)
-
EntityManager.php (modified) (8 diffs)
-
EntityManagerFactory.php (modified) (5 diffs)
-
EntityPersister/Standard.php (modified) (1 diff)
-
EventListener.php (modified) (1 diff)
-
EventManager.php (modified) (2 diffs)
-
Exception.php (modified) (1 diff)
-
HydratorNew.php (modified) (2 diffs)
-
Manager.php (modified) (1 diff)
-
Query.php (modified) (4 diffs)
-
Query/Abstract.php (modified) (1 diff)
-
Query/Exception.php (modified) (2 diffs)
-
Sequence.php (modified) (1 diff)
-
Transaction.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Doctrine/ClassMetadata.php
r4523 r4628 142 142 143 143 /** 144 * Enter description here... 145 * 146 * @var array 147 */ 148 protected $_attributes = array('loadReferences' => true); 149 150 /** 144 151 * An array of field names. used to look up field names from column names. 145 152 * Keys are column names and values are field names. … … 149 156 */ 150 157 protected $_fieldNames = array(); 151 152 /**153 * Enter description here...154 *155 * @var unknown_type156 */157 protected $_attributes = array('loadReferences' => true);158 158 159 159 /** … … 167 167 168 168 /** 169 * Map that maps lowercased column names to field names. 170 * Mainly used during hydration because Doctrine enforces PDO_CASE_LOWER 171 * for portability. 172 * 173 * @var array 174 */ 175 protected $_lcColumnToFieldNames = array(); 176 177 /** 169 178 * Enter description here... 170 179 * 171 180 * @var unknown_type 172 181 */ 173 protected $_subclassFieldNames = array();182 //protected $_subclassFieldNames = array(); 174 183 175 184 /** … … 193 202 * @var integer 194 203 */ 195 protected $_columnCount;204 //protected $_columnCount; 196 205 197 206 /** … … 541 550 542 551 /** 543 * getFieldName 544 * 545 * returns the field name for a column name 546 * if no field name can be found the column name is returned. 552 * Gets the field name for a column name. 553 * If no field name can be found the column name is returned. 547 554 * 548 555 * @param string $columnName column name … … 556 563 557 564 /** 565 * Gets the field name for a completely lowercased column name. 566 * Mainly used during hydration. 567 * 568 * @param string $lcColumnName 569 * @return string 570 */ 571 public function getFieldNameForLowerColumnName($lcColumnName) 572 { 573 return isset($this->_lcColumnToFieldNames[$lcColumnName]) ? 574 $this->_lcColumnToFieldNames[$lcColumnName] : $lcColumnName; 575 } 576 577 public function hasLowerColumn($lcColumnName) 578 { 579 return isset($this->_lcColumnToFieldNames[$lcColumnName]); 580 } 581 582 /** 583 * Looks up the field name for a (lowercased) column name. 558 584 * 559 */ 560 public function lookupFieldName($columnName) 561 { 562 if (isset($this->_fieldNames[$columnName])) { 563 return $this->_fieldNames[$columnName]; 564 } else if (isset($this->_subclassFieldNames[$columnName])) { 565 return $this->_subclassFieldNames[$columnName]; 566 } 585 * This is mostly used during hydration, because we want to make the 586 * conversion to field names while iterating over the result set for best 587 * performance. By doing this at that point, we can avoid re-iterating over 588 * the data just to convert the column names to field names. 589 * 590 * However, when this is happening, we don't know the real 591 * class name to instantiate yet (the row data may target a sub-type), hence 592 * this method looks up the field name in the subclass mappings if it's not 593 * found on this class mapping. 594 * This lookup on subclasses is costly but happens only *once* for a column 595 * during hydration because the hydrator caches effectively. 596 */ 597 public function lookupFieldName($lcColumnName) 598 { 599 if (isset($this->_lcColumnToFieldNames[$lcColumnName])) { 600 return $this->_lcColumnToFieldNames[$lcColumnName]; 601 }/* else if (isset($this->_subclassFieldNames[$lcColumnName])) { 602 return $this->_subclassFieldNames[$lcColumnName]; 603 }*/ 567 604 568 $classMetadata = $this; 569 $conn = $this->_em; 570 571 foreach ($classMetadata->getSubclasses() as $subClass) { 572 $subClassMetadata = $conn->getClassMetadata($subClass); 573 if ($subClassMetadata->hasColumn($columnName)) { 574 $this->_subclassFieldNames[$columnName] = $subClassMetadata->getFieldName($columnName); 575 return $this->_subclassFieldNames[$columnName]; 605 foreach ($this->getSubclasses() as $subClass) { 606 $subClassMetadata = $this->_em->getClassMetadata($subClass); 607 if ($subClassMetadata->hasLowerColumn($lcColumnName)) { 608 /*$this->_subclassFieldNames[$lcColumnName] = $subClassMetadata-> 609 getFieldNameForLowerColumnName($lcColumnName); 610 return $this->_subclassFieldNames[$lcColumnName];*/ 611 return $subClassMetadata->getFieldNameForLowerColumnName($lcColumnName); 576 612 } 577 613 } 578 614 579 throw new Doctrine_ClassMetadata_Exception("No field name found for column name '$ columnName' during lookup.");615 throw new Doctrine_ClassMetadata_Exception("No field name found for column name '$lcColumnName' during lookup."); 580 616 } 581 617 … … 616 652 } 617 653 618 // extract column name & field name 654 // extract column name & field name & lowercased column name 619 655 $parts = explode(' as ', $name); 620 656 if (count($parts) > 1) { … … 623 659 $fieldName = $parts[0]; 624 660 } 625 $name = strtolower($parts[0]); 626 627 if (isset($this->_columnNames[$fieldName])) { 661 $columnName = $parts[0]; 662 $lcColumnName = strtolower($parts[0]); 663 664 if (isset($this->_mappedColumns[$columnName])) { 628 665 return; 629 666 } 630 667 668 // Fill column name <-> field name lookup maps 631 669 if ($prepend) { 632 $this->_columnNames = array_merge(array($fieldName => $ name), $this->_columnNames);633 $this->_fieldNames = array_merge(array($ name => $fieldName), $this->_fieldNames);670 $this->_columnNames = array_merge(array($fieldName => $columnName), $this->_columnNames); 671 $this->_fieldNames = array_merge(array($columnName => $fieldName), $this->_fieldNames); 634 672 } else { 635 $this->_columnNames[$fieldName] = $name; 636 $this->_fieldNames[$name] = $fieldName; 637 } 673 $this->_columnNames[$fieldName] = $columnName; 674 $this->_fieldNames[$columnName] = $fieldName; 675 } 676 $this->_lcColumnToFieldNames[$lcColumnName] = $fieldName; 677 638 678 639 679 // Inspect & fill $options … … 663 703 664 704 if ($prepend) { 665 $this->_mappedColumns = array_merge(array($ name => $options), $this->_mappedColumns);705 $this->_mappedColumns = array_merge(array($columnName => $options), $this->_mappedColumns); 666 706 } else { 667 $this->_mappedColumns[$ name] = $options;707 $this->_mappedColumns[$columnName] = $options; 668 708 } 669 709 -
trunk/lib/Doctrine/Collection.php
r4523 r4628 66 66 * @var Doctrine_Entity 67 67 */ 68 protected $ reference;68 protected $_owner; 69 69 70 70 /** … … 94 94 * @var Doctrine_Null 95 95 */ 96 protected static $null; 96 //protected static $null; 97 98 /** 99 * The EntityManager. 100 * 101 * @var EntityManager 102 */ 103 protected $_em; 97 104 98 105 /** … … 105 112 public function __construct($entityBaseType, $keyField = null) 106 113 { 107 if (is_string($entityBaseType)) { 108 $this->_entityBaseType = $entityBaseType; 109 $mapper = Doctrine_EntityManagerFactory::getManager($entityBaseType) 110 ->getEntityPersister($entityBaseType); 111 } 112 $this->_mapper = $mapper; 114 $this->_entityBaseType = $entityBaseType; 115 $this->_em = Doctrine_EntityManagerFactory::getManager($entityBaseType); 116 $this->_mapper = $this->_em->getEntityPersister($entityBaseType); 113 117 114 118 if ($keyField === null) { 115 $keyField = $ mapper->getClassMetadata()->getBoundQueryPart('indexBy');119 $keyField = $this->_mapper->getClassMetadata()->getBoundQueryPart('indexBy'); 116 120 } 117 121 … … 126 130 $this->_keyField = $keyField; 127 131 } 128 }129 130 /**131 * initNullObject132 * Initializes the null object for this collection.133 *134 * @return void135 */136 public static function initNullObject(Doctrine_Null $null)137 {138 self::$null = $null;139 }140 141 /**142 * getTable143 * Returns the table of the mapper of the collection.144 *145 * @return Doctrine_Table146 */147 public function getTable()148 {149 return $this->_mapper->getTable();150 }151 152 /**153 * getMapper154 * Returns the mapper of this collection.155 *156 * @return Doctrine_Mapper157 */158 public function getMapper()159 {160 return $this->_mapper;161 132 } 162 133 … … 307 278 308 279 /** 309 * setReference280 * INTERNAL: 310 281 * sets a reference pointer 311 282 * 312 283 * @return void 313 284 */ 314 public function setReference(Doctrine_Entity $ record, Doctrine_Relation $relation)315 { 316 $this-> reference = $record;285 public function setReference(Doctrine_Entity $entity, Doctrine_Relation $relation) 286 { 287 $this->_owner = $entity; 317 288 $this->relation = $relation; 318 289 … … 320 291 $relation instanceof Doctrine_Relation_LocalKey) { 321 292 $this->referenceField = $relation->getForeignFieldName(); 322 $value = $ record->get($relation->getLocalFieldName());323 foreach ($this->data as $ record) {293 $value = $entity->get($relation->getLocalFieldName()); 294 foreach ($this->data as $entity) { 324 295 if ($value !== null) { 325 $ record->set($this->referenceField, $value, false);296 $entity->set($this->referenceField, $value, false); 326 297 } else { 327 $ record->set($this->referenceField, $this->reference, false);298 $entity->set($this->referenceField, $this->_owner, false); 328 299 } 329 300 } … … 334 305 335 306 /** 307 * INTERNAL: 336 308 * getReference 337 309 * … … 340 312 public function getReference() 341 313 { 342 return $this->reference; 343 } 344 345 /** 346 * remove 347 * removes a specified collection element 314 return $this->_owner; 315 } 316 317 /** 318 * Removes an entity from the collection. 348 319 * 349 320 * @param mixed $key … … 358 329 359 330 /** 360 * contains 361 * whether or not this collection contains a specified element 331 * Checks whether the collection contains an entity. 362 332 * 363 333 * @param mixed $key the key of the element … … 379 349 380 350 /** 381 * get382 351 * returns a record for given key 383 *384 * There are two special cases:385 *386 * 1. if null is given as a key a new record is created and attached387 * at the end of the collection388 *389 * 2. if given key does not exist, then a new record is create and attached390 * to the given key391 352 * 392 353 * Collection also maps referential information to newly created records … … 397 358 public function get($key) 398 359 { 399 if ( ! isset($this->data[$key])) { 400 $record = $this->_mapper->create(); 401 402 if (isset($this->referenceField)) { 403 $value = $this->reference->get($this->relation->getLocalFieldName()); 404 405 if ($value !== null) { 406 $record->set($this->referenceField, $value, false); 407 } else { 408 $record->set($this->referenceField, $this->reference, false); 409 } 410 } 411 412 if ($key === null) { 413 $this->data[] = $record; 414 } else { 415 $this->data[$key] = $record; 416 } 417 418 if (isset($this->_keyField)) { 419 $record->set($this->_keyField, $key); 420 } 421 422 return $record; 423 } 424 425 return $this->data[$key]; 360 if (isset($this->data[$key])) { 361 return $this->data[$key]; 362 } 363 return null; 426 364 } 427 365 … … 492 430 * to adhere to the Doctrine_Access::set() signature. 493 431 */ 494 public function set($key, $ record)495 { 496 if ( ! $ record instanceOf Doctrine_Entity) {432 public function set($key, $entity) 433 { 434 if ( ! $entity instanceof Doctrine_Entity) { 497 435 throw new Doctrine_Collection_Exception('Value variable in set is not an instance of Doctrine_Entity'); 498 436 } 499 437 500 438 if (isset($this->referenceField)) { 501 $ record->set($this->referenceField, $this->reference, false);502 } 503 $this->data[$key] = $ record;439 $entity->set($this->referenceField, $this->_owner, false); 440 } 441 $this->data[$key] = $entity; 504 442 } 505 443 … … 518 456 519 457 if (isset($this->referenceField)) { 520 $value = $this-> reference->get($this->relation->getLocalFieldName());458 $value = $this->_owner->get($this->relation->getLocalFieldName()); 521 459 522 460 if ($value !== null) { 523 461 $record->set($this->referenceField, $value, false); 524 462 } else { 525 $record->set($this->referenceField, $this-> reference, false);463 $record->set($this->referenceField, $this->_owner, false); 526 464 } 527 465 } … … 560 498 561 499 /** 500 * INTERNAL: 562 501 * loadRelated 563 502 * … … 579 518 } 580 519 } 581 $query->from($this->_mapper->getComponentName() . '(' . implode(", ",$this->_mapper->getTable()->getPrimaryKeys()) . ')'); 582 $query->where($this->_mapper->getComponentName() . '.id IN (' . substr(str_repeat("?, ", count($list)),0,-2) . ')'); 520 $query->from($this->_mapper->getComponentName() 521 . '(' . implode(", ",$this->_mapper->getTable()->getPrimaryKeys()) . ')'); 522 $query->where($this->_mapper->getComponentName() 523 . '.id IN (' . substr(str_repeat("?, ", count($list)),0,-2) . ')'); 583 524 584 525 return $query; … … 608 549 609 550 /** 551 * INTERNAL: 610 552 * populateRelated 611 553 * … … 930 872 $this->data = array(); 931 873 932 if ($this-> reference) {933 $this-> reference->free($deep);934 $this-> reference= null;874 if ($this->_owner) { 875 $this->_owner->free($deep); 876 $this->_owner = null; 935 877 } 936 878 } -
trunk/lib/Doctrine/Configuration.php
r4523 r4628 28 28 * It combines all configuration options from DBAL & ORM. 29 29 * 30 * @author Roman Borschel <roman@code-factory.org> 30 31 * @since 2.0 31 32 */ -
trunk/lib/Doctrine/Connection.php
r4523 r4628 79 79 * The PDO database handle. 80 80 * 81 * @var PDO 82 * @todo Rename to $pdo. 83 */ 84 protected $dbh; 81 * @var PDO 82 */ 83 protected $_pdo; 85 84 86 85 /** … … 117 116 * @var string $driverName 118 117 */ 119 protected $ driverName;118 protected $_driverName; 120 119 121 120 /** … … 124 123 * @var boolean $isConnected 125 124 */ 126 protected $ isConnected = false;125 protected $_isConnected = false; 127 126 128 127 /** … … 164 163 * @var array $availableDrivers 165 164 */ 166 private static $ availableDrivers = array(165 private static $_availableDrivers = array( 167 166 'Mysql', 'Pgsql', 'Oracle', 'Informix', 'Mssql', 'Sqlite', 'Firebird' 168 167 ); … … 173 172 * @var integer 174 173 */ 175 protected $_ count = 0;174 protected $_queryCount = 0; 176 175 177 176 … … 221 220 * Constructor. 222 221 * 223 * @param Doctrine_Manager $manager the manager object 224 * @param PDO|Doctrine_Adapter_Interface $adapter database driver 222 * @param array $params The connection parameters. 225 223 */ 226 224 public function __construct(array $params) 227 225 { 228 226 if (isset($params['pdo'])) { 229 $this-> dbh= $params['pdo'];230 $this-> isConnected = true;227 $this->_pdo = $params['pdo']; 228 $this->_isConnected = true; 231 229 } 232 230 $this->_params = $params; … … 320 318 321 319 /** 322 * getDriverName323 *324 320 * Gets the name of the instance driver 325 321 * … … 328 324 public function getDriverName() 329 325 { 330 return $this-> driverName;326 return $this->_driverName; 331 327 } 332 328 … … 335 331 * 336 332 * @return PDO the database handler 333 * @deprecated 337 334 */ 338 335 public function getDbh() 339 336 { 340 //$this->connect(); 341 342 return $this->dbh; 337 $this->connect(); 338 return $this->_pdo; 339 } 340 341 /** 342 * Gets the PDO handle used by the connection. 343 * 344 * @return PDO 345 */ 346 public function getPdo() 347 { 348 $this->connect(); 349 return $this->_pdo; 343 350 } 344 351 … … 350 357 public function connect() 351 358 { 352 if ($this-> isConnected) {359 if ($this->_isConnected) { 353 360 return false; 354 361 } … … 357 364 //$this->getListener()->preConnect($event); 358 365 359 $e = explode(':', $this->options['dsn']);366 // TODO: the extension_loaded check can happen earlier, maybe in the factory 360 367 if (extension_loaded('pdo')) { 361 if (in_array($e[0], PDO::getAvailableDrivers())) { 362 $this->dbh = new PDO( 363 $this->options['dsn'], $this->options['username'], 364 $this->options['password'], $this->options['other']); 365 $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 366 $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); 367 } 368 $driverOptions = isset($this->_params['driverOptions']) ? 369 $this->_params['driverOptions'] : array(); 370 $user = isset($this->_params['user']) ? 371 $this->_params['user'] : null; 372 $password = isset($this->_params['password']) ? 373 $this->_params['password'] : null; 374 $this->_pdo = new PDO( 375 $this->_constructPdoDsn(), 376 $user, 377 $password, 378 $driverOptions 379 ); 380 $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 381 $this->_pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); 368 382