| 447 | | return Doctrine_Facade::loadAllRuntimeClasses(); |
| | 447 | return self::loadAllRuntimeClasses(); |
| | 448 | } |
| | 449 | |
| | 450 | /** |
| | 451 | * importSchema |
| | 452 | * method for importing existing schema to Doctrine_Record classes |
| | 453 | * |
| | 454 | * @param string $directory Directory to write your models to |
| | 455 | * @param array $databases Array of databases to generate models for |
| | 456 | * @return boolean |
| | 457 | */ |
| | 458 | public static function importSchema($directory, array $databases = array()) |
| | 459 | { |
| | 460 | return self::generateModelsFromDb($directory, $databases); |
| | 461 | } |
| | 462 | |
| | 463 | /** |
| | 464 | * exportSchema |
| | 465 | * method for exporting Doctrine_Record classes to a schema |
| | 466 | * |
| | 467 | * @param string $directory Directory containing your models |
| | 468 | * @return void |
| | 469 | */ |
| | 470 | public static function exportSchema($directory = null) |
| | 471 | { |
| | 472 | return self::createTablesFromModels($directory); |
| | 473 | } |
| | 474 | |
| | 475 | /** |
| | 476 | * exportSql |
| | 477 | * method for exporting Doctrine_Record classes to a schema |
| | 478 | * |
| | 479 | * @param string $directory |
| | 480 | */ |
| | 481 | public static function exportSql($directory = null) |
| | 482 | { |
| | 483 | return self::generateSqlFromModels($directory); |
| | 484 | } |
| | 485 | |
| | 486 | /** |
| | 487 | * loadAllRuntimeClasses |
| | 488 | * |
| | 489 | * loads all runtime classes |
| | 490 | * |
| | 491 | * @return void |
| | 492 | */ |
| | 493 | public static function loadAllRuntimeClasses() |
| | 494 | { |
| | 495 | $classes = Doctrine_Compiler::getRuntimeClasses(); |
| | 496 | |
| | 497 | foreach ($classes as $class) { |
| | 498 | self::autoload($class); |
| | 499 | } |
| 497 | | public static function importSchema($directory, array $databases = array()) |
| 498 | | { |
| 499 | | return Doctrine_Facade::generateModelsFromDb($directory, $databases); |
| 500 | | } |
| 501 | | |
| 502 | | /** |
| 503 | | * exportSchema |
| 504 | | * method for exporting Doctrine_Record classes to a schema |
| | 605 | public static function generateModelsFromDb($directory, array $databases = array()) |
| | 606 | { |
| | 607 | return Doctrine_Manager::connection()->import->importSchema($directory, $databases); |
| | 608 | } |
| | 609 | |
| | 610 | /** |
| | 611 | * generateYamlFromDb |
| | 612 | * |
| | 613 | * Generates models from database to temporary location then uses those models to generate a yaml schema file. |
| | 614 | * This should probably be fixed. We should write something to generate a yaml schema file directly from the database. |
| | 615 | * |
| | 616 | * @param string $yamlPath Path to write oyur yaml schema file to |
| | 617 | * @return void |
| | 618 | */ |
| | 619 | public static function generateYamlFromDb($yamlPath) |
| | 620 | { |
| | 621 | $directory = '/tmp/tmp_doctrine_models'; |
| | 622 | |
| | 623 | Doctrine::generateModelsFromDb($directory); |
| | 624 | |
| | 625 | $export = new Doctrine_Export_Schema(); |
| | 626 | |
| | 627 | $result = $export->exportSchema($yamlPath, 'yml', $directory); |
| | 628 | |
| | 629 | exec('rm -rf ' . $directory); |
| | 630 | |
| | 631 | return $result; |
| | 632 | } |
| | 633 | /** |
| | 634 | * generateModelsFromYaml |
| | 635 | * |
| | 636 | * Generate a yaml schema file from an existing directory of models |
| | 637 | * |
| | 638 | * @param string $yamlPath Path to your yaml schema files |
| | 639 | * @param string $directory Directory to generate your models in |
| | 640 | * @return void |
| | 641 | */ |
| | 642 | public static function generateModelsFromYaml($yamlPath, $directory) |
| | 643 | { |
| | 644 | $import = new Doctrine_Import_Schema(); |
| | 645 | $import->generateBaseClasses(true); |
| | 646 | |
| | 647 | return $import->importSchema($yamlPath, 'yml', $directory); |
| | 648 | } |
| | 649 | |
| | 650 | /** |
| | 651 | * createTablesFromModels |
| | 652 | * |
| | 653 | * Creates database tables for the models in the specified directory |
| 509 | | public static function exportSchema($directory = null) |
| 510 | | { |
| 511 | | return Doctrine_Facade::createTablesFromModels($directory); |
| 512 | | } |
| 513 | | |
| 514 | | /** |
| 515 | | * exportSql |
| 516 | | * method for exporting Doctrine_Record classes to a schema |
| 517 | | * |
| 518 | | * @param string $directory |
| 519 | | */ |
| 520 | | public static function exportSql($directory = null) |
| 521 | | { |
| 522 | | return Doctrine_Facade::generateSqlFromModels($directory); |
| | 658 | public static function createTablesFromModels($directory = null) |
| | 659 | { |
| | 660 | return Doctrine_Manager::connection()->export->exportSchema($directory); |
| | 661 | } |
| | 662 | |
| | 663 | /** |
| | 664 | * generateSqlFromModels |
| | 665 | * |
| | 666 | * @param string $directory |
| | 667 | * @return void |
| | 668 | */ |
| | 669 | public static function generateSqlFromModels($directory = null) |
| | 670 | { |
| | 671 | $sql = Doctrine_Manager::connection()->export->exportSql($directory); |
| | 672 | |
| | 673 | $build = ''; |
| | 674 | foreach ($sql as $query) { |
| | 675 | $build .= $query.";\n"; |
| | 676 | } |
| | 677 | |
| | 678 | return $build; |
| | 679 | } |
| | 680 | |
| | 681 | /** |
| | 682 | * generateYamlFromModels |
| | 683 | * |
| | 684 | * Generate yaml schema file for the models in the specified directory |
| | 685 | * |
| | 686 | * @param string $yamlPath Path to your yaml schema files |
| | 687 | * @param string $directory Directory to generate your models in |
| | 688 | * @return void |
| | 689 | */ |
| | 690 | public static function generateYamlFromModels($yamlPath, $directory) |
| | 691 | { |
| | 692 | $export = new Doctrine_Export_Schema(); |
| | 693 | |
| | 694 | return $export->exportSchema($yamlPath, 'yml', $directory); |
| | 695 | } |
| | 696 | |
| | 697 | /** |
| | 698 | * createDatabases |
| | 699 | * |
| | 700 | * Creates databases for connections |
| | 701 | * |
| | 702 | * @param string $specifiedConnections Array of connections you wish to create the database for |
| | 703 | * @return void |
| | 704 | */ |
| | 705 | public static function createDatabases($specifiedConnections) |
| | 706 | { |
| | 707 | if (!is_array($specifiedConnections)) { |
| | 708 | $specifiedConnections = (array) $specifiedConnections; |
| | 709 | } |
| | 710 | |
| | 711 | $connections = Doctrine_Manager::getInstance()->getConnections(); |
| | 712 | |
| | 713 | foreach ($connections as $name => $connection) { |
| | 714 | if (!empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { |
| | 715 | continue; |
| | 716 | } |
| | 717 | |
| | 718 | $connection->export->createDatabase($name); |
| | 719 | } |
| | 720 | } |
| | 721 | |
| | 722 | /** |
| | 723 | * dropDatabases |
| | 724 | * |
| | 725 | * Drops databases for connections |
| | 726 | * |
| | 727 | * @param string $specifiedConnections Array of connections you wish to drop the database for |
| | 728 | * @return void |
| | 729 | */ |
| | 730 | public static function dropDatabases($specifiedConnections = array()) |
| | 731 | { |
| | 732 | if (!is_array($specifiedConnections)) { |
| | 733 | $specifiedConnections = (array) $specifiedConnections; |
| | 734 | } |
| | 735 | |
| | 736 | $connections = Doctrine_Manager::getInstance()->getConnections(); |
| | 737 | |
| | 738 | foreach ($connections as $name => $connection) { |
| | 739 | if (!empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { |
| | 740 | continue; |
| | 741 | } |
| | 742 | |
| | 743 | $connection->export->dropDatabase($name); |
| | 744 | } |
| | 745 | } |
| | 746 | |
| | 747 | /** |
| | 748 | * dumpData |
| | 749 | * |
| | 750 | * Dump data to a yaml fixtures file |
| | 751 | * |
| | 752 | * @param string $yamlPath Path to write the yaml data fixtures to |
| | 753 | * @param string $individualFiles Whether or not to dump data to individual fixtures files |
| | 754 | * @return void |
| | 755 | */ |
| | 756 | public static function dumpData($yamlPath, $individualFiles = false) |
| | 757 | { |
| | 758 | $data = new Doctrine_Data(); |
| | 759 | |
| | 760 | return $data->exportData($yamlPath, 'yml', array(), $individualFiles); |
| | 761 | } |
| | 762 | |
| | 763 | /** |
| | 764 | * loadData |
| | 765 | * |
| | 766 | * Load data from a yaml fixtures file. |
| | 767 | * The output of dumpData can be fed to loadData |
| | 768 | * |
| | 769 | * @param string $yamlPath Path to your yaml data fixtures |
| | 770 | * @param string $append Whether or not to append the data |
| | 771 | * @return void |
| | 772 | */ |
| | 773 | public static function loadData($yamlPath, $append = false) |
| | 774 | { |
| | 775 | $delete = isset($append) ? ($append ? false : true) : true; |
| | 776 | |
| | 777 | if ($delete) |
| | 778 | { |
| | 779 | $models = Doctrine::getLoadedModels(); |
| | 780 | |
| | 781 | foreach ($models as $model) |
| | 782 | { |
| | 783 | $model = new $model(); |
| | 784 | |
| | 785 | $model->getTable()->createQuery()->delete($model)->execute(); |
| | 786 | } |
| | 787 | } |
| | 788 | |
| | 789 | $data = new Doctrine_Data(); |
| | 790 | |
| | 791 | return $data->importData($yamlPath, 'yml'); |
| | 792 | } |
| | 793 | |
| | 794 | /** |
| | 795 | * loadDummyData |
| | 796 | * |
| | 797 | * Populdate your models with dummy data |
| | 798 | * |
| | 799 | * @param string $append Whether or not to append the data |
| | 800 | * @param string $num Number of records to populate |
| | 801 | * @return void |
| | 802 | */ |
| | 803 | public static function loadDummyData($append, $num = 5) |
| | 804 | { |
| | 805 | $delete = isset($append) ? ($append ? false : true) : true; |
| | 806 | |
| | 807 | if ($delete) |
| | 808 | { |
| | 809 | $models = Doctrine::getLoadedModels(); |
| | 810 | |
| | 811 | foreach ($models as $model) |
| | 812 | { |
| | 813 | $model = new $model(); |
| | 814 | |
| | 815 | $model->getTable()->createQuery()->delete($model)->execute(); |
| | 816 | } |
| | 817 | } |
| | 818 | |
| | 819 | $data = new Doctrine_Data(); |
| | 820 | |
| | 821 | return $data->importDummyData($num); |
| | 822 | } |
| | 823 | |
| | 824 | /** |
| | 825 | * migrate |
| | 826 | * |
| | 827 | * Migrate database to specified $to version. Migrates from current to latest if you do not specify. |
| | 828 | * |
| | 829 | * @param string $directory Directory which contains your migration classes |
| | 830 | * @param string $to Version you wish to migrate to. |
| | 831 | * @return void |
| | 832 | */ |
| | 833 | public static function migrate($directory, $to = null) |
| | 834 | { |
| | 835 | $migration = new Doctrine_Migration($directory); |
| | 836 | |
| | 837 | return $migration->migrate($to); |
| | 838 | } |
| | 839 | |
| | 840 | /** |
| | 841 | * generateMigrationClass |
| | 842 | * |
| | 843 | * Generate new migration class skeleton |
| | 844 | * |
| | 845 | * @param string $className Name of the Migration class to generate |
| | 846 | * @param string $directory Directory which contains your migration classes |
| | 847 | * @package default |
| | 848 | */ |
| | 849 | public static function generateMigrationClass($className, $directory) |
| | 850 | { |
| | 851 | $migration = new Doctrine_Migration($directory); |
| | 852 | $next = (string) $migration->getNextVersion(); |
| | 853 | |
| | 854 | $fileName = str_repeat('0', (3 - strlen($next))) . $next . '_' . self::tableize($className) . '.class.php'; |
| | 855 | $path = $directory . DIRECTORY_SEPARATOR . $fileName; |
| | 856 | |
| | 857 | $code = '<?php' . PHP_EOL; |
| | 858 | $code .= "// Automatically generated by the Doctrine ORM Framework\n"; |
| | 859 | $code .= "class " . self::classify($className) . " extends Doctrine_Migration\n"; |
| | 860 | $code .= "{\n"; |
| | 861 | $code .= "\tpublic function up()\n\t{ }\n\n"; |
| | 862 | $code .= "\tpublic function down()\n\t{ }\n"; |
| | 863 | $code .= "}"; |
| | 864 | |
| | 865 | file_put_contents($path, $code); |