/** * Creates the database schema for the given array of ClassMetadata instances. * * @throws ToolsException * @param array $classes * @return void */ public function createSchema(array $classes) { $createSchemaSql = $this->getCreateSchemaSql($classes); $conn = $this->em->getConnection(); foreach ($createSchemaSql as $sql) { try { $conn->executeQuery($sql); } catch (\Exception $e) { throw ToolsException::schemaToolFailure($sql, $e); } } }
/** * @param string $className * @param string $name * @param string|array $column * @param ClassMetadataInfo $metadata * * @return array * * @throws ToolsException */ private function convertColumn($className, $name, $column, ClassMetadataInfo $metadata) { if (is_string($column)) { $string = $column; $column = array(); $column['type'] = $string; } if (!isset($column['name'])) { $column['name'] = $name; } // check if a column alias was used (column_name as field_name) if (preg_match("/(\\w+)\\sas\\s(\\w+)/i", $column['name'], $matches)) { $name = $matches[1]; $column['name'] = $name; $column['alias'] = $matches[2]; } if (preg_match("/([a-zA-Z]+)\\(([0-9]+)\\)/", $column['type'], $matches)) { $column['type'] = $matches[1]; $column['length'] = $matches[2]; } $column['type'] = strtolower($column['type']); // check if legacy column type (1.x) needs to be mapped to a 2.0 one if (isset($this->legacyTypeMap[$column['type']])) { $column['type'] = $this->legacyTypeMap[$column['type']]; } if (!Type::hasType($column['type'])) { throw ToolsException::couldNotMapDoctrine1Type($column['type']); } $fieldMapping = array(); if (isset($column['primary'])) { $fieldMapping['id'] = true; } $fieldMapping['fieldName'] = isset($column['alias']) ? $column['alias'] : $name; $fieldMapping['columnName'] = $column['name']; $fieldMapping['type'] = $column['type']; if (isset($column['length'])) { $fieldMapping['length'] = $column['length']; } $allowed = array('precision', 'scale', 'unique', 'options', 'notnull', 'version'); foreach ($column as $key => $value) { if (in_array($key, $allowed)) { $fieldMapping[$key] = $value; } } $metadata->mapField($fieldMapping); if (isset($column['autoincrement'])) { $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); } elseif (isset($column['sequence'])) { $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE); $definition = array('sequenceName' => is_array($column['sequence']) ? $column['sequence']['name'] : $column['sequence']); if (isset($column['sequence']['size'])) { $definition['allocationSize'] = $column['sequence']['size']; } if (isset($column['sequence']['value'])) { $definition['initialValue'] = $column['sequence']['value']; } $metadata->setSequenceGeneratorDefinition($definition); } return $fieldMapping; }