/** * Loads all model classes and returns an array of model names. * * @return array An array of model names */ protected function loadModels() { Doctrine_Core::loadModels($this->configuration->getModelDirs()); $models = Doctrine_Core::getLoadedModels(); $models = Doctrine_Core::initializeModels($models); $models = Doctrine_Core::filterInvalidModels($models); return $models; }
public function exportClassesSql(array $classes) { $models = Doctrine_Core::filterInvalidModels($classes); $sql = array(); foreach ($models as $name) { $record = new $name(); $table = $record->getTable(); //$parents = $table->getOption('joinedParents'); // Don't export the tables with attribute EXPORT_NONE' if ($table->getAttribute(Doctrine_Core::ATTR_EXPORT) === Doctrine_Core::EXPORT_NONE) { continue; } $data = $table->getExportableFormat(); $data['options']['inheritance'] = array(); $parents = array("Doctrine_Record", "sfPostgresDoctrineRecord", "sfDoctrineRecord", "BaseDoctrineRecord"); if (get_parent_class($name) != $name && strpos($name, 'Base') === false) { if (strpos(get_parent_class($name), 'Base') === false) { if (!in_array(get_parent_class($name), $parents)) { $data['options']['inheritance'] = get_parent_class($name); } } else { if (get_parent_class(get_parent_class($name)) != get_parent_class($name) && !in_array(get_parent_class(get_parent_class($name)), $parents)) { $data['options']['inheritance'] = get_parent_class(get_parent_class($name)); } } } if (!empty($data['options']['inheritance'])) { $dataTmp = $table->getConnection()->getTable($data['options']['inheritance'])->getExportableFormat(); foreach ($dataTmp['columns'] as $name => $column) { if (isset($data['columns'][$name]) && isset($data['columns'][$name]['primary']) && $data['columns'][$name]['primary'] != 1) { unset($data['columns'][$name]); } } } else { unset($data['options']['inheritance']); } $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_Core::ATTR_EXPORT) & Doctrine_Core::EXPORT_PLUGINS) { $sql = array_merge($sql, $this->exportGeneratorsSql($table)); } // DC-474: Remove dummy $record from repository to not pollute it during export $table->getRepository()->evict($record->getOid()); unset($record); } $sql = array_unique($sql); rsort($sql); return $sql; }
public function testAggressiveModelLoading() { $path = realpath('ModelLoadingTest/Aggressive'); $models = Doctrine_Core::loadModels($path, Doctrine_Core::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_Core::filterInvalidModels($models); // Make sure filterInvalidModels filters out base abstract classes $this->assertTrue(!isset($models['BaseAggressiveModelLoadingUser'])); }
/** * 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_Core::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_Core::filterInvalidModels(Doctrine_Core::loadModels($directory)); } else { $models = Doctrine_Core::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_Core::filterInvalidModels(Doctrine_Core::loadModels($modelsPath, $modelLoading)); } else { $models = Doctrine_Core::getLoadedModels(); } $models = Doctrine_Core::initializeModels($models); $foreignKeys = array(); foreach ($models as $model) { $table = Doctrine_Core::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; }
/** * Loads all Doctrine builders. */ protected function loadModels() { Doctrine_Core::loadModels($this->generatorManager->getConfiguration()->getModelDirs()); $models = Doctrine_Core::getLoadedModels(); $models = Doctrine_Core::initializeModels($models); $models = Doctrine_Core::filterInvalidModels($models); $this->models = $this->filterModels($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_Core::filterInvalidModels($models); } else { $models = Doctrine_Core::getLoadedModels(); } $connections = array(); foreach ($models as $model) { $connections[Doctrine_Core::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_Core::getTable($model)->createQuery()->delete()->execute(); } } }
/** * buildSchema * * Build schema array that can be dumped to file * * @param string $directory The directory of models to build the schema from * @param array $models The array of model names to build the schema for * @param integer $modelLoading The model loading strategy to use to load the models from the passed directory * @return void */ public function buildSchema($directory = null, $models = array(), $modelLoading = null) { if ($directory !== null) { $loadedModels = Doctrine_Core::filterInvalidModels(Doctrine_Core::loadModels($directory, $modelLoading)); } else { $loadedModels = Doctrine_Core::getLoadedModels(); } $array = array(); $parent = new ReflectionClass('Doctrine_Record'); $sql = array(); $fks = array(); $behaviors = array(); foreach (sfProjectConfiguration::getActive()->getPluginPaths() as $path) { if (strpos($path, 'sfPostgresDoctrinePlugin') !== false) { $behaviors = sfYaml::load(sfConfig::get('sf_config_dir') . DIRECTORY_SEPARATOR . 'plugins/sfPostgresDoctrinePlugin' . DIRECTORY_SEPARATOR . 'behaviors.yml'); break; } } // 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_Core::getTable($className); $data = $recordTable->getExportableFormat(); $table = array(); $table['connection'] = $recordTable->getConnection()->getName(); $remove = array('ptype', 'ntype', 'alltypes'); // Fix explicit length in schema, concat it to type in this format: type(length) $columns = array_keys($data['columns']); $tb = array(); foreach ((array) $behaviors as $name => $behavior) { $i = false; if (isset($behavior['tableName'])) { if (is_array($behavior['tableName'])) { if (in_array($data['tableName'], $behavior['tableName'])) { $i = true; } else { foreach ($behavior['tableName'] as $tName) { if (preg_match("/^" . str_replace(array(".", "*"), array("\\.", ".+?"), $tName) . "\$/i", $data['tableName'], $m)) { $i = true; break; } } } } else { if ($behavior['tableName'] == 'all' || $behavior['tableName'] == $data['tableName'] || preg_match("/^" . str_replace(array(".", "*"), array("\\.", ".+?"), $behavior['tableName']) . "\$/i", $data['tableName'], $m)) { $i = true; } } if ($i && isset($behavior['exclusions'])) { if (is_array($behavior['exclusions'])) { foreach ($behavior['exclusions'] as $tName) { if (preg_match("/^" . str_replace(array(".", "*"), array("\\.", ".*?"), $tName) . "\$/i", $data['tableName'], $m)) { $i = false; break; } } } else { if (preg_match("/^" . str_replace(array(".", "*"), array("\\.", ".*?"), $behavior['exclusions']) . "\$/i", $data['tableName'], $m)) { $i = false; } } } $cantHave = array(); $mustHave = array(); if ($i) { if (isset($behavior['condition']) && count($behavior['condition']) > 0 && isset($behavior['condition']['columns'])) { if (is_array($behavior['condition']['columns'])) { if (count($behavior['condition']['columns']) > 0) { foreach ($behavior['condition']['columns'] as $column) { if (preg_match('/^!/i', $column, $m)) { $cantHave[] = str_replace("!", "", $column); } else { $mustHave[] = $column; } } $isOK = true; if (count($mustHave > 0)) { if (count(array_intersect($columns, $mustHave)) != count($mustHave)) { $isOK = false; } } if ($isOK && count($cantHave > 0)) { if (count(array_intersect($columns, $cantHave)) > 0) { $isOK = false; } } if ($isOK) { if (array_key_exists('params', $behavior)) { if (is_array($behavior['params']) && count($behavior['params']) > 0) { $tb[$name] = isset($behavior['params']['all']) ? $behavior['params']['all'] : array(); foreach ($behavior['params'] as $tName => $p) { if ($tName != 'all' && ($data['tableName'] == $tName || preg_match("/^" . str_replace(array(".", "*"), array("\\.", ".*?"), $tName) . "\$/i", $data['tableName'], $m))) { $tb[$name] = $this->arrayMerge($tb[$name], $p); } } } else { $tb[$name] = array(); } foreach ($mustHave as $col) { unset($data['columns'][$col]); } } } } } else { if ($behavior['condition']['columns'] != '') { if (preg_match('/^\\!/i', $behavior['condition']['columns'], $m)) { $cantHave[] = str_replace("!", "", $behavior['condition']['columns']); } else { $mustHave[] = $behavior['condition']['columns']; } $isOK = true; if (count($mustHave > 0)) { if (count(array_intersect($columns, $mustHave)) != count($mustHave)) { $isOK = false; } } if ($isOK && count($cantHave > 0)) { if (count(array_intersect($columns, $cantHave)) > 0) { $isOK = false; } } if ($isOK) { if (is_array($behavior['params']) && count($behavior['params']) > 0) { $tb[$name] = isset($behavior['params']['all']) ? $behavior['params']['all'] : array(); foreach ($behavior['params'] as $tName => $p) { if ($tName != 'all' && ($data['tableName'] == $tName || preg_match("/^" . str_replace(array(".", "*"), array("\\.", ".*?"), $tName) . "\$/i", $data['tableName'], $m))) { $tb[$name] = $this->arrayMerge($tb[$name], $p); } } } else { $tb[$name] = array(); } foreach ($mustHave as $col) { unset($data['columns'][$col]); } } } else { $i = false; } } } else { if (array_key_exists('params', $behavior)) { if (is_array($behavior['params']) && count($behavior['params']) > 0) { $tb[$name] = isset($behavior['params']['all']) ? $behavior['params']['all'] : array(); foreach ($behavior['params'] as $tName => $p) { //if ($name == 'Callback') echo $tName.'--'.$data['tableName'].'--'.preg_match("/^".str_replace(array(".", "*"), array("\\.", ".*?"), $tName)."$/i", $data['tableName'], $m)."\n"; if ($tName != 'all' && ($data['tableName'] == $tName || preg_match("/^" . str_replace(array(".", "*"), array("\\.", ".*?"), $tName) . "\$/i", $data['tableName'], $m))) { $tb[$name] = $this->arrayMerge($tb[$name], $p); } } //$tb[$name] = array_merge(isset($behavior['params']['all']) ? $behavior['params']['all'] : array(), isset($behavior['params'][$data['tableName']]) ? $behavior['params'][$data['tableName']] : array()); } else { $tb[$name] = array(); } } else { $tb[$name] = array(); } } } } } if (count($tb) > 0) { $table['actAs'] = $tb; } 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']; if (count($data['columns']) > 0) { $table['columns'] = $data['columns']; } $e = explode('.', $table['tableName']); if (2 == count($e)) { $table['package'] = ucfirst($e[0]) . '.' . 'Entities'; } if (get_parent_class($className) != $className && strpos($className, 'Base') === false) { if (strpos(get_parent_class($className), 'Base') === false) { if (get_parent_class($className) != "Doctrine_Record") { $table['inheritance'] = array('extends' => get_parent_class($className)); foreach ($table['columns'] as $key => $val) { if (true == $val['primary']) { unset($table['columns'][$key]); } } } } else { if (get_parent_class(get_parent_class($className)) != get_parent_class($className) && get_parent_class(get_parent_class($className)) != "Doctrine_Record") { $table['inheritance'] = array('extends' => get_parent_class(get_parent_class($className))); foreach ($table['columns'] as $key => $val) { if (1 == $val['primary']) { unset($table['columns'][$key]); } } } } } $relations = $recordTable->getRelations(); // unset inherited relation $keys = array_keys($relations); if (get_parent_class($className) != 'Doctrine_Record' && get_parent_class($className) != $className) { if (strpos(get_parent_class($className), 'Base') === false) { $keys = array_diff(array_keys($relations), array_keys(Doctrine_Core::getTable(get_parent_class($className))->getRelations())); } else { if (get_parent_class(get_parent_class($className)) != 'Doctrine_Record' && get_parent_class(get_parent_class($className)) != get_parent_class($className) && strpos(get_parent_class(get_parent_class($className)), 'Base') === false) { $keys = array_diff(array_keys($relations), array_keys(Doctrine_Core::getTable(get_parent_class(get_parent_class($className)))->getRelations())); } } } foreach ($relations as $key => $relation) { if (in_array($key, $keys)) { $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; }
/** * buildSchema * * Build schema array that can be dumped to file * * @param string $directory The directory of models to build the schema from * @param array $models The array of model names to build the schema for * @param integer $modelLoading The model loading strategy to use to load the models from the passed directory * @return void */ public function buildSchema($directory = null, $models = array(), $modelLoading = null) { if ($directory !== null) { $loadedModels = Doctrine_Core::filterInvalidModels(Doctrine_Core::loadModels($directory, $modelLoading)); } else { $loadedModels = Doctrine_Core::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_Core::getTable($className); $data = $recordTable->getExportableFormat(); $table = array(); $table['connection'] = $recordTable->getConnection()->getName(); $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; }
/** * Load builders * * @return array Loaded models */ protected function loadModels() { Doctrine_Core::loadModels(array(sfConfig::get('sf_lib_dir') . '/model')); $this->models = $this->filterModels(Doctrine_Core::filterInvalidModels(Doctrine_Core::initializeModels(Doctrine_Core::getLoadedModels()))); return $this->models; }