function _startMigration($id, $direction) { $migration = $this->migrations[$id]; if ($migration['format'] == 'yml') { $yml = $this->_parsePhp(MIGRATIONS_PATH . DS . $migration['filename']); $array = Yaml::Load($yml); if (!is_array($array)) { return "Unable to parse YAML Migration file"; } $direction = strtoupper($direction); } else { include MIGRATIONS_PATH . DS . $migration['filename']; $array = $migration; unset($migration); } if (!$array[$direction]) { return "Direction does not exist!"; } return $this->_array2Sql($array[$direction]); }
function main() { $this->out(); $models = count($this->args) ? $this->args : '*'; $folder = new Folder(DATA_FILES_PATH, true, 0777); list($dirs, $files) = $folder->read(); if (!$files) { $this->out($this->_colorize("You have not yet created any data files for your database tables.", 'COMMENT')); $this->out($this->_colorize("Run '", 'COMMENT') . 'cake populate create' . $this->_colorize("' to create empty data files for each", 'COMMENT')); $this->out($this->_colorize("table in your database.", 'COMMENT')); $this->out(); $this->hr(); $this->out(); exit; } require 'populate_helpers.php'; $this->helpers = new PopulateHelpers(); if ($models == '*') { $models = $this->uses; } else { $this->_user_defined = true; } foreach ((array) $models as $name) { $name = Inflector::classify($name); if (!in_array($name, $this->uses)) { if (!$this->_user_defined && !isset($this->params['verbose']) && !isset($this->params['v'])) { continue; } $this->out("Populating model '" . $name . "' ...", false); $this->out("\n " . $this->_colorize("FAIL", 'ERROR') . $this->_colorize(" model '{$name}' does not exist", 'COMMENT')); continue; } $file = Inflector::tableize($name); if (!file_exists(DATA_FILES_PATH . DS . $file . '.yml')) { if (!$this->_user_defined && !isset($this->params['verbose']) && !isset($this->params['v'])) { continue; } $this->out("Populating model '" . $name . "' ...", false); $this->out("\n " . $this->_colorize("FAIL", 'ERROR') . $this->_colorize(" {$name}.yml does not exist", 'COMMENT')); continue; } $data = Yaml::Load($this->_parsePhp(DATA_FILES_PATH . DS . $file . '.yml')); if (!is_array($data) || !count($data)) { if (!$this->_user_defined && !isset($this->params['verbose']) && !isset($this->params['v'])) { continue; } $this->out("Populating model '" . $name . "' ...", false); $this->out("\n " . $this->_colorize("FAIL", 'ERROR') . $this->_colorize(" unable to parse YAML. Maybe empty.", 'COMMENT')); continue; } if ($data = $this->_startFixture($name, $data)) { $this->data[$name] = $data; } } foreach ($this->data as $name => $records) { foreach ($records as $i => $record) { foreach ($record as $key => $val) { $class = Inflector::classify($key); if ($val == '.RANDOM') { if (isset($this->data[$class])) { $this->data[$name][$i][$key . '_id'] = $this->data[$class][array_rand($this->data[$class])]['id']; unset($this->data[$name][$i][$key]); } else { $this->out("Populating model '" . $name . "' ...", false); $this->out("\n " . $this->_colorize("FAIL", 'ERROR') . $this->_colorize(" Data file for association '{$class}' not loaded/found.", 'COMMENT')); $this->out(); $this->hr(); exit; } } } } } foreach ($this->data as $name => $records) { $this->{$name}->deleteAll(array('1=1'), false); $this->out("Populating model '" . $name . "' ...", false); $res = $this->{$name}->saveAll($records, array('validate' => false)); $this->out($this->_colorize(count($records) . ' rows inserted.', 'COMMENT')); } $this->out(); $this->hr(); $this->out(); }