Show
Ignore:
Timestamp:
10/11/07 04:23:33 (15 months ago)
Author:
jwage
Message:

Fleshing out functionality of CLI system and changed syntax of migration classes to only required a to parameter since we already know where we are coming from.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/Doctrine.php

    r2735 r2809  
    518518    } 
    519519    /** 
     520     * generateModelsFromDb 
     521     * 
     522     * Generate your model definitions from an existing database 
     523     * 
     524     * @param string $directory  
     525     * @param string $array  
     526     * @return void 
     527     */ 
     528    public static function generateModelsFromDb($directory, array $databases = array()) 
     529    { 
     530        return self::importSchema($directory, $databases); 
     531    } 
     532    /** 
     533     * generateYamlFromDb 
     534     * 
     535     * Generates models from database to temporary location then uses those models to generate a yaml schema file. 
     536     * This should probably be fixed. We should write something to generate a yaml schema file directly from the database. 
     537     * 
     538     * @param string $yamlPath  
     539     * @return void 
     540     */ 
     541    public static function generateYamlFromDb($yamlPath) 
     542    { 
     543        $directory = '/tmp/tmp_doctrine_models'; 
     544 
     545        Doctrine::generateModelsFromDb($directory); 
     546 
     547        $export = new Doctrine_Export_Schema(); 
     548         
     549        return $export->exportSchema($yamlPath, 'yml', $directory); 
     550    } 
     551    /** 
     552     * generateModelsFromYaml 
     553     * 
     554     * Generate a yaml schema file from an existing directory of models 
     555     * 
     556     * @param string $yamlPath  
     557     * @param string $directory  
     558     * @return void 
     559     */ 
     560    public static function generateModelsFromYaml($yamlPath, $directory) 
     561    { 
     562        $import = new Doctrine_Import_Schema(); 
     563         
     564        return $import->importSchema($yamlPath, 'yml', $directory); 
     565    } 
     566    /** 
    520567     * exportSchema 
    521568     * method for exporting Doctrine_Record classes to a schema 
     
    528575    } 
    529576    /** 
     577     * createTablesFromModels 
     578     * 
     579     * Creates database tables for the models in the specified directory 
     580     * 
     581     * @param string $directory  
     582     * @return void 
     583     */ 
     584    public static function createTablesFromModels($directory = null) 
     585    { 
     586        return self::exportSchema($directory); 
     587    } 
     588    /** 
     589     * generateYamlFromModels 
     590     * 
     591     * Generate yaml schema file for the models in the specified directory 
     592     * 
     593     * @param string $yamlPath  
     594     * @param string $directory  
     595     * @return void 
     596     */ 
     597    public static function generateYamlFromModels($yamlPath, $directory) 
     598    { 
     599        $export = new Doctrine_Export_Schema(); 
     600         
     601        return $export->exportSchema($yamlPath, 'yml', $directory); 
     602    } 
     603    /** 
     604     * createDatabases 
     605     * 
     606     * Creates databases for connections 
     607     * 
     608     * @param string $specifiedConnections  
     609     * @return void 
     610     */ 
     611    public static function createDatabases($specifiedConnections) 
     612    { 
     613        if (!is_array($specifiedConnections)) { 
     614            $specifiedConnections = (array) $specifiedConnections; 
     615        } 
     616         
     617        $connections = Doctrine_Manager::getInstance()->getConnections(); 
     618         
     619        foreach ($connections as $name => $connection) { 
     620            if (!empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { 
     621                continue; 
     622            } 
     623             
     624            $connection->export->createDatabase($name); 
     625        } 
     626    } 
     627    /** 
     628     * dropDatabases 
     629     * 
     630     * Drops databases for connections 
     631     * 
     632     * @param string $specifiedConnections  
     633     * @return void 
     634     */ 
     635    public static function dropDatabases($specifiedConnections = array()) 
     636    { 
     637        if (!is_array($specifiedConnections)) { 
     638            $specifiedConnections = (array) $specifiedConnections; 
     639        } 
     640         
     641        $connections = Doctrine_Manager::getInstance()->getConnections(); 
     642         
     643        foreach ($connections as $name => $connection) { 
     644            if (!empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { 
     645                continue; 
     646            } 
     647             
     648            $connection->export->dropDatabase($name); 
     649        } 
     650    } 
     651    /** 
     652     * dumpData 
     653     * 
     654     * Dump data to a yaml fixtures file 
     655     * 
     656     * @param string $yamlPath  
     657     * @param string $individualFiles  
     658     * @return void 
     659     */ 
     660    public static function dumpData($yamlPath, $individualFiles = false) 
     661    { 
     662        $data = new Doctrine_Data(); 
     663         
     664        return $data->exportData($yamlPath, 'yml', array(), $individualFiles); 
     665    } 
     666    /** 
     667     * loadData 
     668     * 
     669     * Load data from a yaml fixtures file. 
     670     * The output of dumpData can be fed to loadData 
     671     * 
     672     * @param string $yamlPath  
     673     * @param string $append  
     674     * @return void 
     675     */ 
     676    public static function loadData($yamlPath, $append = false) 
     677    { 
     678        $delete = isset($append) ? ($append ? false : true) : true; 
     679 
     680        if ($delete) 
     681        { 
     682          $models = Doctrine::getLoadedModels(); 
     683 
     684          foreach ($models as $model) 
     685          { 
     686            $model = new $model(); 
     687 
     688            $model->getTable()->createQuery()->delete($model)->execute(); 
     689          } 
     690        } 
     691 
     692        $data = new Doctrine_Data(); 
     693         
     694        return $data->importData($yamlPath, 'yml'); 
     695    } 
     696    /** 
     697     * loadDummyData 
     698     * 
     699     * Populdate your models with dummy data 
     700     * 
     701     * @param string $append  
     702     * @param string $num  
     703     * @return void 
     704     */ 
     705    public static function loadDummyData($append, $num = 5) 
     706    { 
     707        $delete = isset($append) ? ($append ? false : true) : true; 
     708 
     709        if ($delete) 
     710        { 
     711          $models = Doctrine::getLoadedModels(); 
     712 
     713          foreach ($models as $model) 
     714          { 
     715            $model = new $model(); 
     716 
     717            $model->getTable()->createQuery()->delete($model)->execute(); 
     718          } 
     719        } 
     720         
     721        $data = new Doctrine_Data(); 
     722         
     723        return $data->importDummyData($num); 
     724    } 
     725     
     726    public static function migrate($directory, $to = null) 
     727    { 
     728        $migration = new Doctrine_Migration($directory); 
     729         
     730        return $migration->migrate($to); 
     731    } 
     732     
     733    public static function generateMigrationClass($className, $directory) 
     734    { 
     735        $migration = new Doctrine_Migration($directory); 
     736        $next = (string) $migration->getNextVersion(); 
     737         
     738        $fileName = str_repeat('0', (3 - strlen($next))) . $next . '_' . Doctrine::tableize($className) . '.class.php'; 
     739        $path = $directory . DIRECTORY_SEPARATOR . $fileName; 
     740         
     741        $code  = '<?php' . PHP_EOL; 
     742        $code .= "// Automatically generated by Doctrine\n"; 
     743        $code .= "class " . $className . " extends Doctrine_Migration\n"; 
     744        $code .= "{\n"; 
     745        $code .= "\tpublic function up()\n\t{ }\n\n"; 
     746        $code .= "\tpublic function down()\n\t{ }\n"; 
     747        $code .= "}"; 
     748         
     749        file_put_contents($path, $code); 
     750    } 
     751     
     752    /** 
    530753     * exportSql 
    531754     * method for exporting Doctrine_Record classes to a schema 
     
    536759    { 
    537760        return Doctrine_Manager::connection()->export->exportSql($directory); 
     761    } 
     762    public static function generateSqlFromModels($directory) 
     763    { 
     764        return self::exportSql($directory); 
    538765    } 
    539766    /**