/** * importObj * * A method to import a XML Schema and translate it into a Doctrine_Record object * * @param string $schema The file containing the XML schema * @param string $directory The directory where the Doctrine_Record classes will * be written */ public function importObj($schema, $directory) { $builder = new Doctrine_Import_Builder(); $builder->setTargetPath($directory); $arr = $this->importArr($schema); foreach ($arr as $name => $columns) { $Builder->buildRecord($name, $columns); } }
/** * importSchema * * method for importing existing schema to Doctrine_Record classes * * @param string $directory * @param array $databases * @return array the names of the imported classes */ public function importSchema($directory, array $databases = array(), array $options = array()) { $options['singularize'] = !isset($options['singularize']) ? $this->conn->getAttribute('singularize_import') : $options['singularize']; $connections = Doctrine_Manager::getInstance()->getConnections(); foreach ($connections as $name => $connection) { // Limit the databases to the ones specified by $databases. // Check only happens if array is not empty if (!empty($databases) && !in_array($name, $databases)) { continue; } $builder = new Doctrine_Import_Builder(); $builder->setTargetPath($directory); $builder->setOptions($options); $classes = array(); foreach ($connection->import->listTables() as $table) { $definition = array(); $definition['tableName'] = $table; // echo $table . "<br />\n"; if (!isset($options['singularize']) || $options['singularize'] !== false) { $e = explode('_', Doctrine_Inflector::tableize($table)); foreach ($e as $k => $v) { $e[$k] = Doctrine_Inflector::singularize($v); } $classTable = implode('_', $e); } else { $classTable = Doctrine_Inflector::tableize($table); } $definition['className'] = Doctrine_Inflector::classify($classTable); $definition['columns'] = $connection->import->listTableColumns($table); //var_dump ($definition); $builder->buildRecord($definition); $classes[] = $definition['className']; } } return $classes; }
/** * importSchema * * method for importing existing schema to Doctrine_Record classes * * @param string $directory * @param array $connections Array of connection names to generate models for * @return array the names of the imported classes */ public function importSchema($directory, array $connections = array(), array $options = array()) { $classes = array(); $manager = Doctrine_Manager::getInstance(); foreach ($manager as $name => $connection) { // Limit the databases to the ones specified by $connections. // Check only happens if array is not empty if (!empty($connections) && !in_array($name, $connections)) { continue; } $builder = new Doctrine_Import_Builder(); $builder->setTargetPath($directory); $builder->setOptions($options); $definitions = array(); foreach ($connection->import->listTables() as $table) { $definition = array(); $definition['tableName'] = $table; $definition['className'] = Doctrine_Inflector::classify(Doctrine_Inflector::tableize($table)); $definition['columns'] = $connection->import->listTableColumns($table); $definition['connection'] = $connection->getName(); $definition['connectionClassName'] = $definition['className']; try { $definition['relations'] = array(); $relations = $connection->import->listTableRelations($table); $relClasses = array(); foreach ($relations as $relation) { $table = $relation['table']; $class = Doctrine_Inflector::classify(Doctrine_Inflector::tableize($table)); if (in_array($class, $relClasses)) { $alias = $class . '_' . (count($relClasses) + 1); } else { $alias = $class; } $relClasses[] = $class; $definition['relations'][$alias] = array('alias' => $alias, 'class' => $class, 'local' => $relation['local'], 'foreign' => $relation['foreign']); } } catch (Exception $e) { } $definitions[strtolower($definition['className'])] = $definition; $classes[] = $definition['className']; } // Build opposite end of relationships foreach ($definitions as $definition) { $className = $definition['className']; $relClasses = array(); foreach ($definition['relations'] as $alias => $relation) { if (in_array($relation['class'], $relClasses) || isset($definitions[$relation['class']]['relations'][$className])) { $alias = $className . '_' . (count($relClasses) + 1); } else { $alias = $className; } $relClasses[] = $relation['class']; $definitions[strtolower($relation['class'])]['relations'][$alias] = array('type' => Doctrine_Relation::MANY, 'alias' => $alias, 'class' => $className, 'local' => $relation['foreign'], 'foreign' => $relation['local']); } } // Build records foreach ($definitions as $definition) { $builder->buildRecord($definition); } } return $classes; }
/** * Generates the class definition for plugin class * * @param array $definition Definition array defining columns, relations and options * for the model * @return void */ public function generateClass(array $definition = array()) { $definition['className'] = $this->_options['className']; $definition['toString'] = isset($this->_options['toString']) ? $this->_options['toString'] : false; if (isset($this->_options['listeners'])) { $definition['listeners'] = $this->_options['listeners']; } $builder = new Doctrine_Import_Builder(); $builderOptions = isset($this->_options['builderOptions']) ? (array) $this->_options['builderOptions'] : array(); $builder->setOptions($builderOptions); if ($this->_options['generateFiles']) { if (isset($this->_options['generatePath']) && $this->_options['generatePath']) { $builder->setTargetPath($this->_options['generatePath']); $builder->buildRecord($definition); } else { throw new Doctrine_Record_Exception('If you wish to generate files then you must specify the path to generate the files in.'); } } else { $def = $builder->buildDefinition($definition); eval($def); } }
/** * importSchema * * A method to import a Schema and translate it into a Doctrine_Record object * * @param string $schema The file containing the XML schema * @param string $format Format of the schema file * @param string $directory The directory where the Doctrine_Record class will be written * @param array $models Optional array of models to import * * @return void */ public function importSchema($schema, $format = 'yml', $directory = null, $models = array()) { $schema = (array) $schema; $builder = new Doctrine_Import_Builder(); $builder->setTargetPath($directory); $builder->setOptions($this->getOptions()); $array = $this->buildSchema($schema, $format); if (count($array) == 0) { throw new Doctrine_Import_Exception(sprintf('No ' . $format . ' schema found in ' . implode(", ", $schema))); } foreach ($array as $name => $definition) { if (!empty($models) && !in_array($definition['className'], $models)) { continue; } $builder->buildRecord($definition); } }
/** * importSchema * * A method to import a Schema and translate it into a Doctrine_Record object * * @param string $schema The file containing the XML schema * @param string $format Format of the schema file * @param string $directory The directory where the Doctrine_Record class will be written * @param array $models Optional array of models to import * * @return void */ public function importSchema($schema, $format = 'yml', $directory = null, $models = array()) { $builder = new Doctrine_Import_Builder(); $builder->setTargetPath($directory); $builder->setOptions($this->getOptions()); $array = $this->buildSchema($schema, $format); foreach ($array as $name => $definition) { if (!empty($models) && !in_array($definition['className'], $models)) { continue; } $builder->buildRecord($definition); } }
/** * @param Doctrine_Import_Builder $builder * @param array $definition */ protected function _buildRecord($builder, $definition) { $className = $definition['className']; if (strpos($className, "Model_") === false) { throw ZFDoctrine_DoctrineException::invalidZendModel($className); } $classPrefix = substr($className, 0, strpos($className, 'Model_') + strlen('Model_')); $camelCaseToDash = new Zend_Filter_Word_CamelCaseToDash(); $moduleName = current(explode("_", $className)); $moduleName = strtolower($camelCaseToDash->filter($moduleName)); if (!isset($this->_modules[$moduleName])) { throw ZFDoctrine_DoctrineException::unknownModule($moduleName, $className); } $builder->setTargetPath($this->_modules[$moduleName]); $builder->buildRecord($definition); if ($this->_listener) { $this->_listener->notifyRecordBuilt($className, $moduleName); } }
/** * importSchema * * A method to import a Schema and translate it into a Doctrine_Record object * * @param string $schema The file containing the XML schema * @param string $directory The directory where the Doctrine_Record class will be written * @param array $models Optional array of models to import * * @return void */ public function importSchema($schema, $format = 'yml', $directory = null, $models = array()) { $builder = new Doctrine_Import_Builder(); $builder->setTargetPath($directory); foreach ($this->_options as $key => $value) { if ($value) { $builder->setOption($key, $value); } } $array = $this->buildSchema($schema, $format); foreach ($array as $name => $properties) { if (!empty($models) && !in_array($properties['className'], $models)) { continue; } $options = $this->getOptions($properties); $columns = $this->getColumns($properties); $relations = $this->getRelations($properties); $indexes = $this->getIndexes($properties); $attributes = $this->getAttributes($properties); $templates = $this->getTemplates($properties); $actAs = $this->getActAs($properties); $tableOptions = $this->getTableOptions($properties); $builder->buildRecord($options, $columns, $relations, $indexes, $attributes, $templates, $actAs, $tableOptions); } }
/** * generates the class definition for plugin class * * @param array $columns the plugin class columns, keys representing the column names * and values as column definitions * * @param array $relations the bound relations of the plugin class * * @param array $options plugin class options, keys representing the option names * and values as option values * @return void */ public function generateClass(array $columns = array(), array $relations = array(), array $options = array()) { $options['className'] = $this->_options['className']; $builder = new Doctrine_Import_Builder(); if ($this->_options['generateFiles']) { if (isset($this->_options['generatePath']) && $this->_options['generatePath']) { $builder->setTargetPath($this->_options['generatePath']); $builder->buildRecord($options, $columns, $relations); } else { throw new Doctrine_Plugin_Exception('If you wish to generate files then you must specify the path to generate the files in.'); } } else { $def = $builder->buildDefinition($options, $columns, $relations); eval($def); } }
/** * importSchema * * method for importing existing schema to Doctrine_Record classes * * @param string $directory * @param array $databases * @return array the names of the imported classes */ public function importSchema($directory, array $databases = array(), array $options = array()) { $connections = Doctrine_Manager::getInstance()->getConnections(); foreach ($connections as $name => $connection) { // Limit the databases to the ones specified by $databases. // Check only happens if array is not empty if (!empty($databases) && !in_array($name, $databases)) { continue; } $builder = new Doctrine_Import_Builder(); $builder->setTargetPath($directory); $builder->setOptions($options); $classes = array(); foreach ($connection->import->listTables() as $table) { $builder->buildRecord(array('tableName' => $table, 'className' => Doctrine::classify($table)), $connection->import->listTableColumns($table), array()); $classes[] = Doctrine::classify($table); } } return $classes; }
/** * import * * method for importing existing schema to Doctrine_Record classes * * @param string $directory * @param array $databases * @return array the names of the imported classes */ public function import($directory, array $databases = array()) { $builder = new Doctrine_Import_Builder(); $builder->setTargetPath($directory); $classes = array(); foreach ($this->listTables() as $table) { $builder->buildRecord($table, $this->listTableColumns($table)); $classes[] = Doctrine::classify($table); } return $classes; }