/** * Provides a array with all model names * * @return array */ public function loadModels() { $models = Doctrine::loadModels($this->modelDir, Doctrine::MODEL_LOADING_CONSERVATIVE); $models = Doctrine::initializeModels($models); $this->models = Doctrine::filterInvalidModels($models); return $this->models; }
public function testAggressiveModelLoading() { $path = realpath('ModelLoadingTest/Aggressive'); $models = Doctrine::loadModels($path, Doctrine::MODEL_LOADING_AGGRESSIVE); // Ensure the correct model names were returned $this->assertTrue(isset($models['AggressiveModelLoadingUser']) && $models['AggressiveModelLoadingUser'] == 'AggressiveModelLoadingUser'); $this->assertTrue(isset($models['AggressiveModelLoadingProfile']) && $models['AggressiveModelLoadingProfile'] == 'AggressiveModelLoadingProfile'); $this->assertTrue(isset($models['AggressiveModelLoadingContact']) && $models['AggressiveModelLoadingContact'] == 'AggressiveModelLoadingContact'); // Make sure it does not include the base classes $this->assertTrue(!isset($models['BaseAggressiveModelLoadingUser'])); $filteredModels = Doctrine::filterInvalidModels($models); // Make sure filterInvalidModels filters out base abstract classes $this->assertTrue(!isset($models['BaseAggressiveModelLoadingUser'])); }
/** * buildSchema * * Build schema array that can be dumped to file * * @param string $directory * @return void */ public function buildSchema($directory = null, $models = array()) { if ($directory !== null) { $loadedModels = Doctrine::filterInvalidModels(Doctrine::loadModels($directory)); } else { $loadedModels = Doctrine::getLoadedModels(); } $array = array(); $parent = new ReflectionClass('Doctrine_Record'); $sql = array(); $fks = array(); // we iterate through the diff of previously declared classes // and currently declared classes foreach ($loadedModels as $className) { if (!empty($models) && !in_array($className, $models)) { continue; } $recordTable = Doctrine::getTable($className); $data = $recordTable->getExportableFormat(); $table = array(); $remove = array('ptype', 'ntype', 'alltypes'); // Fix explicit length in schema, concat it to type in this format: type(length) foreach ($data['columns'] as $name => $column) { if (isset($column['length']) && $column['length'] && isset($column['scale']) && $column['scale']) { $data['columns'][$name]['type'] = $column['type'] . '(' . $column['length'] . ', ' . $column['scale'] . ')'; unset($data['columns'][$name]['length'], $data['columns'][$name]['scale']); } else { $data['columns'][$name]['type'] = $column['type'] . '(' . $column['length'] . ')'; unset($data['columns'][$name]['length']); } // Strip out schema information which is not necessary to be dumped to the yaml schema file foreach ($remove as $value) { if (isset($data['columns'][$name][$value])) { unset($data['columns'][$name][$value]); } } // If type is the only property of the column then lets abbreviate the syntax // columns: { name: string(255) } if (count($data['columns'][$name]) === 1 && isset($data['columns'][$name]['type'])) { $type = $data['columns'][$name]['type']; unset($data['columns'][$name]); $data['columns'][$name] = $type; } } $table['tableName'] = $data['tableName']; $table['columns'] = $data['columns']; $relations = $recordTable->getRelations(); foreach ($relations as $key => $relation) { $relationData = $relation->toArray(); $relationKey = $relationData['alias']; if (isset($relationData['refTable']) && $relationData['refTable']) { $table['relations'][$relationKey]['refClass'] = $relationData['refTable']->getComponentName(); } if (isset($relationData['class']) && $relationData['class'] && $relation['class'] != $relationKey) { $table['relations'][$relationKey]['class'] = $relationData['class']; } $table['relations'][$relationKey]['local'] = $relationData['local']; $table['relations'][$relationKey]['foreign'] = $relationData['foreign']; if ($relationData['type'] === Doctrine_Relation::ONE) { $table['relations'][$relationKey]['type'] = 'one'; } else { if ($relationData['type'] === Doctrine_Relation::MANY) { $table['relations'][$relationKey]['type'] = 'many'; } else { $table['relations'][$relationKey]['type'] = 'one'; } } } $array[$className] = $table; } return $array; }
/** * purge * * Purge all data for loaded models or for the passed array of Doctrine_Records * * @param string $models * @return void */ public function purge($models = array()) { $models = Doctrine::filterInvalidModels($models); foreach ($models as $model) { $model = new $model(); $model->getTable()->createQuery()->delete($model)->execute(); } }
/** * Loads all Doctrine builders. */ protected function loadModels() { Doctrine::loadModels($this->generatorManager->getConfiguration()->getModelDirs(), Doctrine::MODEL_LOADING_CONSERVATIVE); $models = Doctrine::getLoadedModels(); $models = Doctrine::initializeModels($models); $this->models = Doctrine::filterInvalidModels($models); return $this->models; }
/** * purge * * Purge all data for loaded models or for the passed array of Doctrine_Records * * @param string $models * @return void */ public function purge($models = null) { if ($models) { $models = Doctrine::filterInvalidModels($models); } else { $models = Doctrine::getLoadedModels(); } foreach ($models as $model) { $model = new $model(); $model->getTable()->createQuery()->delete($model)->execute(); } }
/** * generateMigrationsFromModels * * @param string $modelsPath * @return void */ public function generateMigrationsFromModels($modelsPath = null, $modelLoading = null) { if ($modelsPath !== null) { $models = Doctrine::filterInvalidModels(Doctrine::loadModels($modelsPath, $modelLoading)); } else { $models = Doctrine::getLoadedModels(); } $models = Doctrine::initializeModels($models); $foreignKeys = array(); foreach ($models as $model) { $export = Doctrine::getTable($model)->getExportableFormat(); $foreignKeys[$export['tableName']] = $export['options']['foreignKeys']; $up = $this->buildCreateTable($export); $down = $this->buildDropTable($export); $className = 'Add' . Doctrine_Inflector::classify($export['tableName']); $this->generateMigrationClass($className, array(), $up, $down); } if (!empty($foreignKeys)) { $className = 'ApplyForeignKeyConstraints'; $up = ''; $down = ''; foreach ($foreignKeys as $tableName => $definitions) { $tableForeignKeyNames[$tableName] = array(); foreach ($definitions as $definition) { $definition['name'] = $tableName . '_' . implode('_', (array) $definition['local']); $up .= $this->buildCreateForeignKey($tableName, $definition); $down .= $this->buildDropForeignKey($tableName, $definition); } } $this->generateMigrationClass($className, array(), $up, $down); } return true; }
/** * exportSql * returns the sql for exporting Doctrine_Record classes to a schema * * if the directory parameter is given this method first iterates * recursively trhough the given directory in order to find any model classes * * Then it iterates through all declared classes and creates tables for the ones * that extend Doctrine_Record and are not abstract classes * * @throws Doctrine_Connection_Exception if some error other than Doctrine::ERR_ALREADY_EXISTS * occurred during the create table operation * @param string $directory optional directory parameter * @return void */ public function exportSql($directory = null) { if ($directory !== null) { $models = Doctrine::filterInvalidModels(Doctrine::loadModels($directory)); } else { $models = Doctrine::getLoadedModels(); } return $this->exportSortedClassesSql($models, false); }
/** * Generate a set of migrations from a set of models * * @param string $modelsPath Path to models * @param string $modelLoading What type of model loading to use when loading the models * @return boolean */ public function generateMigrationsFromModels($modelsPath = null, $modelLoading = null) { if ($modelsPath !== null) { $models = Doctrine::filterInvalidModels(Doctrine::loadModels($modelsPath, $modelLoading)); } else { $models = Doctrine::getLoadedModels(); } $models = Doctrine::initializeModels($models); $foreignKeys = array(); foreach ($models as $model) { $table = Doctrine::getTable($model); if ($table->getTableName() !== $this->migration->getTableName()) { $export = $table->getExportableFormat(); $foreignKeys[$export['tableName']] = $export['options']['foreignKeys']; $up = $this->buildCreateTable($export); $down = $this->buildDropTable($export); $className = 'Add' . Doctrine_Inflector::classify($export['tableName']); $this->generateMigrationClass($className, array(), $up, $down); } } if (!empty($foreignKeys)) { $className = 'AddFks'; $up = array(); $down = array(); foreach ($foreignKeys as $tableName => $definitions) { $tableForeignKeyNames[$tableName] = array(); foreach ($definitions as $definition) { $up[] = $this->buildCreateForeignKey($tableName, $definition); $down[] = $this->buildDropForeignKey($tableName, $definition); } } $up = implode("\n", $up); $down = implode("\n", $down); if ($up || $down) { $this->generateMigrationClass($className, array(), $up, $down); } } return true; }
/* * This file is part of sfDoctrineGraphvizPlugin * (c) 2009 David PHAM-VAN * (c) 2009 Dejan Spasic <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once dirname(__FILE__) . '/../../../bootstrap.php'; require_once dirname(__FILE__) . '/../../../../lib/generator/doctrineGraphvizMcdGenerator.class.php'; // path to model directory $modelDir = dirname(__FILE__) . '/../../../_files/model'; // retrieve modelnames $modelNames = Doctrine::loadModels($modelDir, Doctrine::MODEL_LOADING_CONSERVATIVE); $modelNames = Doctrine::initializeModels($modelNames); $modelNames = Doctrine::filterInvalidModels($modelNames); $subject = new doctrineGraphvizMcdGenerator(); $t = new lime_test(8, new lime_output_color()); $t->ok("" === $subject->getBuffer(), "Test if ->getBuffer() is empty"); $t->ok($subject->generate($modelNames) instanceof doctrineGraphvizMcdGenerator, "Test if generate return a object from type doctrineGraphvizMcdGenerator"); $t->ok(is_string($subject->getBuffer()) && "" !== $subject->getBuffer(), "Test if the current buffer is form type string and not empty"); $t->ok($subject->flushBuffer($modelNames) instanceof doctrineGraphvizMcdGenerator, "Test if ->flushBuffer() return a object from type doctrineGraphvizMcdGenerator"); $t->ok("" === $subject->getBuffer(), "Test if ->flushBuffer() flushed the buffer"); $t->ok($subject->generate($modelNames)->getBuffer() === file_get_contents(dirname(__FILE__) . '/../../../_files/expected/mcd.schema.dot'), "Test the generated graphviz"); try { $subject->generate('foo'); $t->fail('Generate did not throw a exception'); } catch (Exception $e) { $t->ok($e instanceof InvalidArgumentException, '->generate throw a InvalidArgumentException'); $t->ok($e->getMessage() === "First Argument must be from type array. [string] given.", "Test the messsage from exception"); }
/** * purge * * Purge all data for loaded models or for the passed array of Doctrine_Records * * @param string $models * @return void */ public function purge($models = null) { if ($models) { $models = Doctrine::filterInvalidModels($models); } else { $models = Doctrine::getLoadedModels(); } $connections = array(); foreach ($models as $model) { $connections[Doctrine::getTable($model)->getConnection()->getName()][] = $model; } foreach ($connections as $connection => $models) { $models = Doctrine_Manager::getInstance()->getConnection($connection)->unitOfWork->buildFlushTree($models); $models = array_reverse($models); foreach ($models as $model) { Doctrine::getTable($model)->createQuery()->delete()->execute(); } } }
/** * exportClassesSql * method for exporting Doctrine_Record classes to a schema * * @throws Doctrine_Connection_Exception if some error other than Doctrine::ERR_ALREADY_EXISTS * occurred during the create table operation * @param array $classes * @return void */ public function exportClassesSql(array $classes) { $models = Doctrine::filterInvalidModels($classes); $sql = array(); foreach ($models as $name) { $record = new $name(); $table = $record->getTable(); $parents = $table->getOption('joinedParents'); foreach ($parents as $parent) { $data = $table->getConnection()->getTable($parent)->getExportableFormat(); $query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']); $sql = array_merge($sql, (array) $query); } $data = $table->getExportableFormat(); $query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']); if (is_array($query)) { $sql = array_merge($sql, $query); } else { $sql[] = $query; } if ($table->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_PLUGINS) { $sql = array_merge($sql, $this->exportPluginsSql($table)); } } $sql = array_unique($sql); rsort($sql); return $sql; }