コード例 #1
0
ファイル: import_db.php プロジェクト: bigSiebs/poetry_vote
function getDbStructure($schema, $model_name = null, $verbose_output = true)
{
    //set active connection to schema and stop if there is no matching schema
    if (!DinklyDataConfig::setActiveConnection($schema)) {
        echo "\nNo such schema in config/db.yml\n";
        return false;
    }
    //Connect to database
    $db = DinklyDataConnector::fetchDB();
    $creds = DinklyDataConfig::getDBCreds();
    $db_name = $creds["name"];
    $db_name = DinklyBuilder::sanitize($db, $db_name);
    $model_name = DinklyBuilder::sanitize($db, $model_name);
    //get columns from specified table or all tables
    if ($model_name) {
        $stmt = $db->prepare("SHOW COLUMNS FROM " . $model_name . "");
        $stmt->execute();
        $table_schema = $stmt->fetchAll();
        echo "\nRead table " . $model_name . " columns...\n";
        sqlToYml($table_schema, $model_name, $db_name);
    } else {
        $stmt = $db->prepare("SHOW TABLES");
        $stmt->execute();
        $table_names = $stmt->fetchAll();
        foreach ($table_names as $table_array) {
            $table_name = $table_array[0];
            $stmt = $db->prepare("SHOW COLUMNS FROM " . $table_name . "");
            $stmt->execute();
            $table_schema = $stmt->fetchAll();
            echo "\nRead table " . $table_name . " columns...\n";
            sqlToYml($table_schema, $table_name, $db_name);
        }
    }
}
コード例 #2
0
 /**
  * Fetch database connection from DB credentials in Config file
  *
  * @param int $error_mode int 0 = no errors, 1 = show warnings, 2 = throw exceptions
  * @return PDO object with chosen error mode
  */
 public static function fetchDB($error_mode = 1)
 {
     $pdo_err_mode = null;
     if ($error_mode == 0) {
         $pdo_err_mode = PDO::ERRMODE_SILENT;
     } else {
         if ($error_mode == 1) {
             $pdo_err_mode = PDO::ERRMODE_WARNING;
         } else {
             if ($error_mode == 2) {
                 $pdo_err_mode = PDO::ERRMODE_EXCEPTION;
             }
         }
     }
     $creds = DinklyDataConfig::getDBCreds();
     $db = new PDO("mysql:host=" . $creds['host'] . ";dbname=" . $creds['name'], $creds['user'], $creds['pass']);
     $db->setAttribute(PDO::ATTR_ERRMODE, $pdo_err_mode);
     return $db;
 }
コード例 #3
0
 public function testGetDBCreds()
 {
     //Make sure the yaml is parsing correctly
     $creds = DinklyDataConfig::getDBCreds();
     $this->assertEquals($this->db_creds, $creds);
 }
コード例 #4
0
 /**
  *  Load a specific fixture to populate DB table
  *
  * @param string $set: folder name of fixtures you would like to load
  * @param string $model_name: name model fixture to be parsed
  * @param bool $truncate (optional): truncate the table if set to true, or append records if false
  * @param bool $verbose_output (optional): how chatty would you like the build to be?
  * @param string $override_database_name (optional): if passed, this will override the name of the database as it appears in config/db.yml
  *
  * @return bool true if loaded successfully, false if load fails
  */
 public static function loadFixture($set, $model_name, $plugin_name = null, $truncate = true, $verbose_output = true, $override_database_name = null)
 {
     //Use the proper DB credentials, or apply a passed-in override
     $creds = DinklyDataConfig::getDBCreds();
     if ($override_database_name) {
         $creds['name'] = $override_database_name;
         DinklyDataConfig::setActiveConnection($creds);
     }
     //Create database if it doesn't exist
     $db = self::fetchDB($creds);
     $file_path = null;
     if ($plugin_name) {
         $file_path = $_SERVER['APPLICATION_ROOT'] . "plugins/" . $plugin_name . "/config/fixtures/" . $set . "/" . $model_name . ".yml";
     } else {
         $file_path = $_SERVER['APPLICATION_ROOT'] . "config/fixtures/" . $set . "/" . $model_name . ".yml";
     }
     if ($verbose_output) {
         echo "Attempting to parse '" . $model_name . "' fixture yaml...";
     }
     $fixture = Yaml::parse($file_path);
     $model_name = $collection_name = null;
     if (isset($fixture['table_name'])) {
         $model_name = Dinkly::convertToCamelCase($fixture['table_name'], true);
         if ($verbose_output) {
             echo "success!\n";
         }
     } else {
         return false;
     }
     if (isset($fixture['records'])) {
         if ($truncate) {
             if ($verbose_output) {
                 echo "Truncating '" . $fixture['table_name'] . "'...";
             }
             $db->exec("truncate table " . $fixture['table_name']);
             if ($verbose_output) {
                 echo "success!\n";
             }
         }
         $count = sizeof($fixture['records']);
         if ($verbose_output) {
             echo "Inserting " . $count . " record(s) into table '" . $fixture['table_name'] . "'";
         }
         foreach ($fixture['records'] as $pos => $record) {
             if ($verbose_output) {
                 echo "...";
             }
             $model = new $model_name();
             foreach ($record as $col_name => $value) {
                 //Automatically set created date if none was passed
                 if ($col_name == 'created_at' && $value == "") {
                     $value = date('Y-m-d G:i:s');
                 }
                 $set_field = 'set' . Dinkly::convertToCamelCase($col_name, true);
                 $model->{$set_field}($value);
             }
             $model->save();
         }
         if ($verbose_output) {
             echo "success!\n";
         }
         return true;
     }
 }