Changeset 2872 for trunk/lib/Doctrine.php
- Timestamp:
- 10/15/07 23:05:35 (15 months ago)
- Files:
-
- 1 modified
-
trunk/lib/Doctrine.php (modified) (32 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Doctrine.php
r2866 r2872 190 190 */ 191 191 const LIMIT_ROWS = 1; 192 192 193 /** 193 194 * constant for record limiting … … 204 205 */ 205 206 const FETCH_IMMEDIATE = 0; 207 206 208 /** 207 209 * BATCH FETCHING … … 209 211 */ 210 212 const FETCH_BATCH = 1; 213 211 214 /** 212 215 * LAZY FETCHING … … 214 217 */ 215 218 const FETCH_OFFSET = 3; 219 216 220 /** 217 221 * LAZY OFFSET FETCHING … … 229 233 */ 230 234 const FETCH_VHOLDER = 1; 235 231 236 /** 232 237 * FETCH RECORD … … 238 243 */ 239 244 const FETCH_RECORD = 2; 245 240 246 /** 241 247 * FETCH ARRAY 242 248 */ 243 244 249 const FETCH_ARRAY = 3; 250 245 251 /** 246 252 * PORTABILITY CONSTANTS … … 252 258 */ 253 259 const PORTABILITY_NONE = 0; 260 254 261 /** 255 262 * Portability: convert names of tables and fields to case defined in the … … 264 271 */ 265 272 const PORTABILITY_RTRIM = 2; 273 266 274 /** 267 275 * Portability: force reporting the number of rows deleted. … … 269 277 */ 270 278 const PORTABILITY_DELETE_COUNT = 4; 279 271 280 /** 272 281 * Portability: convert empty values to null strings in data output by … … 275 284 */ 276 285 const PORTABILITY_EMPTY_TO_NULL = 8; 286 277 287 /** 278 288 * Portability: removes database/table qualifiers from associative indexes … … 280 290 */ 281 291 const PORTABILITY_FIX_ASSOC_FIELD_NAMES = 16; 292 282 293 /** 283 294 * Portability: makes Doctrine_Expression throw exception for unportable RDBMS expressions … … 285 296 */ 286 297 const PORTABILITY_EXPR = 32; 298 287 299 /** 288 300 * Portability: turn on all portability features. … … 299 311 */ 300 312 const LOCK_OPTIMISTIC = 0; 313 301 314 /** 302 315 * mode for pessimistic locking 303 316 */ 304 317 const LOCK_PESSIMISTIC = 1; 318 305 319 /** 306 320 * EXPORT CONSTANTS … … 311 325 */ 312 326 const EXPORT_NONE = 0; 327 313 328 /** 314 329 * export tables 315 330 */ 316 331 const EXPORT_TABLES = 1; 332 317 333 /** 318 334 * export constraints 319 335 */ 320 336 const EXPORT_CONSTRAINTS = 2; 337 321 338 /** 322 339 * export plugins 323 340 */ 324 341 const EXPORT_PLUGINS = 4; 342 325 343 /** 326 344 * export all … … 352 370 */ 353 371 const IDENTIFIER_AUTOINC = 1; 372 354 373 /** 355 374 * constant for sequence identifier 356 375 */ 357 376 const IDENTIFIER_SEQUENCE = 2; 377 358 378 /** 359 379 * constant for normal identifier 360 380 */ 361 381 const IDENTIFIER_NATURAL = 3; 382 362 383 /** 363 384 * constant for composite identifier 364 385 */ 365 386 const IDENTIFIER_COMPOSITE = 4; 366 /** 367 * constructor 387 388 /** 389 * @var string $path doctrine root directory 390 */ 391 private static $_path; 392 393 /** 394 * @var boolean $_debug 395 */ 396 private static $_debug = false; 397 398 /** 399 * __construct 400 * 401 * @return void 402 * @throws Doctrine_Exception 368 403 */ 369 404 public function __construct() … … 371 406 throw new Doctrine_Exception('Doctrine is static class. No instances can be created.'); 372 407 } 373 /** 374 * @var string $path doctrine root directory 375 */ 376 private static $_path; 377 /** 378 * @var boolean $_debug 379 */ 380 private static $_debug = false; 381 408 409 /** 410 * debug 411 * 412 * @param string $bool 413 * @return void 414 */ 382 415 public static function debug($bool = null) 383 416 { … … 385 418 self::$_debug = (bool) $bool; 386 419 } 420 387 421 return self::$_debug; 388 422 } 423 389 424 /** 390 425 * getPath … … 398 433 self::$_path = dirname(__FILE__); 399 434 } 435 400 436 return self::$_path; 401 437 } 438 402 439 /** 403 440 * loadAll … … 408 445 public static function loadAll() 409 446 { 410 $classes = Doctrine_Compiler::getRuntimeClasses(); 411 412 foreach ($classes as $class) { 413 Doctrine::autoload($class); 414 } 415 } 447 return Doctrine_Facade::loadAllRuntimeClasses(); 448 } 449 416 450 /** 417 451 * loadModels … … 424 458 public static function loadModels($directory) 425 459 { 426 $declared = get_declared_classes(); 427 428 if ($directory !== null) { 429 foreach ((array) $directory as $dir) { 430 $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), 431 RecursiveIteratorIterator::LEAVES_ONLY); 432 433 foreach ($it as $file) { 434 $e = explode('.', $file->getFileName()); 435 if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) { 436 require_once $file->getPathName(); 437 } 438 } 439 } 440 441 $declared = array_diff(get_declared_classes(), $declared); 442 } 443 444 return self::getLoadedModels($declared); 445 } 460 return Doctrine_Facade::loadModels($directory); 461 } 462 446 463 /** 447 464 * getLoadedModels … … 454 471 public static function getLoadedModels($classes = null) 455 472 { 456 if ($classes === null) { 457 $classes = get_declared_classes(); 458 } 459 460 $parent = new ReflectionClass('Doctrine_Record'); 461 462 $loadedModels = array(); 463 464 // we iterate trhough the diff of previously declared classes 465 // and currently declared classes 466 foreach ($classes as $name) { 467 $class = new ReflectionClass($name); 468 469 // Skip the following classes 470 // - abstract classes 471 // - not a subclass of Doctrine_Record 472 // - don't have a setTableDefinition method 473 if ($class->isAbstract() || 474 !$class->isSubClassOf($parent) || 475 !$class->hasMethod('setTableDefinition')) { 476 continue; 477 } 478 479 $loadedModels[] = $name; 480 } 481 482 return $loadedModels; 483 } 473 return Doctrine_Facade::getLoadedModels($classes); 474 } 475 484 476 /** 485 477 * getConnectionByTableName … … 492 484 public static function getConnectionByTableName($tableName) 493 485 { 494 $loadedModels = self::getLoadedModels(); 495 496 foreach ($loadedModels as $name) { 497 $model = new $name(); 498 $table = $model->getTable(); 499 500 if ($table->getTableName() == $tableName) { 501 return $table->getConnection(); 502 } 503 } 504 return Doctrine_Manager::connection(); 505 } 486 return Doctrine_Facade::getConnectionByTableName($tableName); 487 } 488 506 489 /** 507 490 * importSchema … … 514 497 public static function importSchema($directory, array $databases = array()) 515 498 { 516 return Doctrine_Manager::connection()->import->importSchema($directory, $databases); 517 } 518 /** 519 * generateModelsFromDb 520 * 521 * Generate your model definitions from an existing database 522 * 523 * @param string $directory Directory to write your models to 524 * @param string $databases Array of databases to generate models for 525 * @return void 526 */ 527 public static function generateModelsFromDb($directory, array $databases = array()) 528 { 529 return self::importSchema($directory, $databases); 530 } 531 /** 532 * generateYamlFromDb 533 * 534 * Generates models from database to temporary location then uses those models to generate a yaml schema file. 535 * This should probably be fixed. We should write something to generate a yaml schema file directly from the database. 536 * 537 * @param string $yamlPath Path to write oyur yaml schema file to 538 * @return void 539 */ 540 public static function generateYamlFromDb($yamlPath) 541 { 542 $directory = '/tmp/tmp_doctrine_models'; 543 544 Doctrine::generateModelsFromDb($directory); 545 546 $export = new Doctrine_Export_Schema(); 547 548 return $export->exportSchema($yamlPath, 'yml', $directory); 549 } 550 /** 551 * generateModelsFromYaml 552 * 553 * Generate a yaml schema file from an existing directory of models 554 * 555 * @param string $yamlPath Path to your yaml schema files 556 * @param string $directory Directory to generate your models in 557 * @return void 558 */ 559 public static function generateModelsFromYaml($yamlPath, $directory) 560 { 561 $import = new Doctrine_Import_Schema(); 562 $import->generateBaseClasses(true); 563 564 return $import->importSchema($yamlPath, 'yml', $directory); 565 } 499 return Doctrine_Facade::generateModelsFromDb($directory, $databases); 500 } 501 566 502 /** 567 503 * exportSchema … … 573 509 public static function exportSchema($directory = null) 574 510 { 575 return Doctrine_Manager::connection()->export->exportSchema($directory); 576 } 577 /** 578 * createTablesFromModels 579 * 580 * Creates database tables for the models in the specified directory 581 * 582 * @param string $directory Directory containing your models 583 * @return void 584 */ 585 public static function createTablesFromModels($directory = null) 586 { 587 return self::exportSchema($directory); 588 } 589 /** 590 * generateYamlFromModels 591 * 592 * Generate yaml schema file for the models in the specified directory 593 * 594 * @param string $yamlPath Path to your yaml schema files 595 * @param string $directory Directory to generate your models in 596 * @return void 597 */ 598 public static function generateYamlFromModels($yamlPath, $directory) 599 { 600 $export = new Doctrine_Export_Schema(); 601 602 return $export->exportSchema($yamlPath, 'yml', $directory); 603 } 604 /** 605 * createDatabases 606 * 607 * Creates databases for connections 608 * 609 * @param string $specifiedConnections Array of connections you wish to create the database for 610 * @return void 611 */ 612 public static function createDatabases($specifiedConnections) 613 { 614 if (!is_array($specifiedConnections)) { 615 $specifiedConnections = (array) $specifiedConnections; 616 } 617 618 $connections = Doctrine_Manager::getInstance()->getConnections(); 619 620 foreach ($connections as $name => $connection) { 621 if (!empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { 622 continue; 623 } 624 625 $connection->export->createDatabase($name); 626 } 627 } 628 /** 629 * dropDatabases 630 * 631 * Drops databases for connections 632 * 633 * @param string $specifiedConnections Array of connections you wish to drop the database for 634 * @return void 635 */ 636 public static function dropDatabases($specifiedConnections = array()) 637 { 638 if (!is_array($specifiedConnections)) { 639 $specifiedConnections = (array) $specifiedConnections; 640 } 641 642 $connections = Doctrine_Manager::getInstance()->getConnections(); 643 644 foreach ($connections as $name => $connection) { 645 if (!empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { 646 continue; 647 } 648 649 $connection->export->dropDatabase($name); 650 } 651 } 652 /** 653 * dumpData 654 * 655 * Dump data to a yaml fixtures file 656 * 657 * @param string $yamlPath Path to write the yaml data fixtures to 658 * @param string $individualFiles Whether or not to dump data to individual fixtures files 659 * @return void 660 */ 661 public static function dumpData($yamlPath, $individualFiles = false) 662 { 663 $data = new Doctrine_Data(); 664 665 return $data->exportData($yamlPath, 'yml', array(), $individualFiles); 666 } 667 /** 668 * loadData 669 * 670 * Load data from a yaml fixtures file. 671 * The output of dumpData can be fed to loadData 672 * 673 * @param string $yamlPath Path to your yaml data fixtures 674 * @param string $append Whether or not to append the data 675 * @return void 676 */ 677 public static function loadData($yamlPath, $append = false) 678 { 679 $delete = isset($append) ? ($append ? false : true) : true; 680 681 if ($delete) 682 { 683 $models = Doctrine::getLoadedModels(); 684 685 foreach ($models as $model) 686 { 687 $model = new $model(); 688 689 $model->getTable()->createQuery()->delete($model)->execute(); 690 } 691 } 692 693 $data = new Doctrine_Data(); 694 695 return $data->importData($yamlPath, 'yml'); 696 } 697 /** 698 * loadDummyData 699 * 700 * Populdate your models with dummy data 701 * 702 * @param string $append Whether or not to append the data 703 * @param string $num Number of records to populate 704 * @return void 705 */ 706 public static function loadDummyData($append, $num = 5) 707 { 708 $delete = isset($append) ? ($append ? false : true) : true; 709 710 if ($delete) 711 { 712 $models = Doctrine::getLoadedModels(); 713 714 foreach ($models as $model) 715 { 716 $model = new $model(); 717 718 $model->getTable()->createQuery()->delete($model)->execute(); 719 } 720 } 721 722 $data = new Doctrine_Data(); 723 724 return $data->importDummyData($num); 725 } 726 /** 727 * migrate 728 * 729 * Migrate database to specified $to version. Migrates from current to latest if you do not specify. 730 * 731 * @param string $directory Directory which contains your migration classes 732 * @param string $to Version you wish to migrate to. 733 * @return void 734 */ 735 public static function migrate($directory, $to = null) 736 { 737 $migration = new Doctrine_Migration($directory); 738 739 return $migration->migrate($to); 740 } 741 /** 742 * generateMigrationClass 743 * 744 * Generate new migration class skeleton 745 * 746 * @param string $className Name of the Migration class to generate 747 * @param string $directory Directory which contains your migration classes 748 * @package default 749 */ 750 public static function generateMigrationClass($className, $directory) 751 { 752 $migration = new Doctrine_Migration($directory); 753 $next = (string) $migration->getNextVersion(); 754 755 $fileName = str_repeat('0', (3 - strlen($next))) . $next . '_' . Doctrine::tableize($className) . '.class.php'; 756 $path = $directory . DIRECTORY_SEPARATOR . $fileName; 757 758 $code = '<?php' . PHP_EOL; 759 $code .= "// Automatically generated by the Doctrine ORM Framework\n"; 760 $code .= "class " . Doctrine::classify($className) . " extends Doctrine_Migration\n"; 761 $code .= "{\n"; 762 $code .= "\tpublic function up()\n\t{ }\n\n"; 763 $code .= "\tpublic function down()\n\t{ }\n"; 764 $code .= "}"; 765 766 file_put_contents($path, $code); 767 } 511 return Doctrine_Facade::createTablesFromModels($directory); 512 } 513 768 514 /** 769 515 * exportSql … … 774 520 public static function exportSql($directory = null) 775 521 { 776 return Doctrine_Manager::connection()->export->exportSql($directory); 777 } 778 /** 779 * generateSqlFromModels 780 * 781 * Generate sql for directory of models 782 * 783 * @param string $directory Directory where your models exist 784 * @return void 785 */ 786 public static function generateSqlFromModels($directory) 787 { 788 return self::exportSql($directory); 789 } 522 return Doctrine_Facade::generateSqlFromModels($directory); 523 } 524 790 525 /** 791 526 * compile … … 801 536 public static function compile($target = null) 802 537 { 803 Doctrine_Compiler::compile($target); 804 } 538 return Doctrine_Facade::compile($target); 539 } 540 805 541 /** 806 542 * simple autoload function … … 815 551 return false; 816 552 } 553 817 554 if ( ! self::$_path) { 818 555 self::$_path = dirname(__FILE__); 819 556 } 820 $class = self::$_path . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR,$classname) . '.php'; 557 558 $class = self::$_path . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $classname) . '.php'; 821 559 822 560 if ( ! file_exists($class)) { … … 828 566 return true; 829 567 } 568 830 569 /** 831 570 * dump … … 859 598 return implode("\n", $ret); 860 599 } 600 861 601 /** 862 602 * returns table name from class name … … 869 609 return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $classname)); 870 610 } 611 871 612 /** 872 613 * returns class name from table name … … 879 620 return preg_replace_callback('~(_?)(_)([\w])~', array("Doctrine", "classifyCallback"), ucfirst($tablename)); 880 621 } 881 622 882 623 /** 883 624 * Callback function to classify a classname propperly. … … 890 631 return $matches[1] . strtoupper($matches[3]); 891 632 } 633 892 634 /** 893 635 * checks for valid class name (uses camel case and underscores)