ConvertDoctrine1Schema.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\ORM\Tools;
  22. use Doctrine\ORM\Mapping\ClassMetadataInfo,
  23. Doctrine\ORM\Tools\Export\Driver\AbstractExporter,
  24. Doctrine\Common\Util\Inflector;
  25. /**
  26. * Class to help with converting Doctrine 1 schema files to Doctrine 2 mapping files
  27. *
  28. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  29. * @link www.doctrine-project.org
  30. * @since 2.0
  31. * @version $Revision$
  32. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  33. * @author Jonathan Wage <jonwage@gmail.com>
  34. * @author Roman Borschel <roman@code-factory.org>
  35. */
  36. class ConvertDoctrine1Schema
  37. {
  38. private $_legacyTypeMap = array(
  39. // TODO: This list may need to be updated
  40. 'clob' => 'text',
  41. 'timestamp' => 'datetime',
  42. 'enum' => 'string'
  43. );
  44. /**
  45. * Constructor passes the directory or array of directories
  46. * to convert the Doctrine 1 schema files from
  47. *
  48. * @param array $from
  49. * @author Jonathan Wage
  50. */
  51. public function __construct($from)
  52. {
  53. $this->_from = (array) $from;
  54. }
  55. /**
  56. * Get an array of ClassMetadataInfo instances from the passed
  57. * Doctrine 1 schema
  58. *
  59. * @return array $metadatas An array of ClassMetadataInfo instances
  60. */
  61. public function getMetadata()
  62. {
  63. $schema = array();
  64. foreach ($this->_from as $path) {
  65. if (is_dir($path)) {
  66. $files = glob($path . '/*.yml');
  67. foreach ($files as $file) {
  68. $schema = array_merge($schema, (array) \Symfony\Component\Yaml\Yaml::parse($file));
  69. }
  70. } else {
  71. $schema = array_merge($schema, (array) \Symfony\Component\Yaml\Yaml::parse($path));
  72. }
  73. }
  74. $metadatas = array();
  75. foreach ($schema as $className => $mappingInformation) {
  76. $metadatas[] = $this->_convertToClassMetadataInfo($className, $mappingInformation);
  77. }
  78. return $metadatas;
  79. }
  80. private function _convertToClassMetadataInfo($className, $mappingInformation)
  81. {
  82. $metadata = new ClassMetadataInfo($className);
  83. $this->_convertTableName($className, $mappingInformation, $metadata);
  84. $this->_convertColumns($className, $mappingInformation, $metadata);
  85. $this->_convertIndexes($className, $mappingInformation, $metadata);
  86. $this->_convertRelations($className, $mappingInformation, $metadata);
  87. return $metadata;
  88. }
  89. private function _convertTableName($className, array $model, ClassMetadataInfo $metadata)
  90. {
  91. if (isset($model['tableName']) && $model['tableName']) {
  92. $e = explode('.', $model['tableName']);
  93. if (count($e) > 1) {
  94. $metadata->table['schema'] = $e[0];
  95. $metadata->table['name'] = $e[1];
  96. } else {
  97. $metadata->table['name'] = $e[0];
  98. }
  99. }
  100. }
  101. private function _convertColumns($className, array $model, ClassMetadataInfo $metadata)
  102. {
  103. $id = false;
  104. if (isset($model['columns']) && $model['columns']) {
  105. foreach ($model['columns'] as $name => $column) {
  106. $fieldMapping = $this->_convertColumn($className, $name, $column, $metadata);
  107. if (isset($fieldMapping['id']) && $fieldMapping['id']) {
  108. $id = true;
  109. }
  110. }
  111. }
  112. if ( ! $id) {
  113. $fieldMapping = array(
  114. 'fieldName' => 'id',
  115. 'columnName' => 'id',
  116. 'type' => 'integer',
  117. 'id' => true
  118. );
  119. $metadata->mapField($fieldMapping);
  120. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
  121. }
  122. }
  123. private function _convertColumn($className, $name, $column, ClassMetadataInfo $metadata)
  124. {
  125. if (is_string($column)) {
  126. $string = $column;
  127. $column = array();
  128. $column['type'] = $string;
  129. }
  130. if ( ! isset($column['name'])) {
  131. $column['name'] = $name;
  132. }
  133. // check if a column alias was used (column_name as field_name)
  134. if (preg_match("/(\w+)\sas\s(\w+)/i", $column['name'], $matches)) {
  135. $name = $matches[1];
  136. $column['name'] = $name;
  137. $column['alias'] = $matches[2];
  138. }
  139. if (preg_match("/([a-zA-Z]+)\(([0-9]+)\)/", $column['type'], $matches)) {
  140. $column['type'] = $matches[1];
  141. $column['length'] = $matches[2];
  142. }
  143. $column['type'] = strtolower($column['type']);
  144. // check if legacy column type (1.x) needs to be mapped to a 2.0 one
  145. if (isset($this->_legacyTypeMap[$column['type']])) {
  146. $column['type'] = $this->_legacyTypeMap[$column['type']];
  147. }
  148. if ( ! \Doctrine\DBAL\Types\Type::hasType($column['type'])) {
  149. throw ToolsException::couldNotMapDoctrine1Type($column['type']);
  150. }
  151. $fieldMapping = array();
  152. if (isset($column['primary'])) {
  153. $fieldMapping['id'] = true;
  154. }
  155. $fieldMapping['fieldName'] = isset($column['alias']) ? $column['alias'] : $name;
  156. $fieldMapping['columnName'] = $column['name'];
  157. $fieldMapping['type'] = $column['type'];
  158. if (isset($column['length'])) {
  159. $fieldMapping['length'] = $column['length'];
  160. }
  161. $allowed = array('precision', 'scale', 'unique', 'options', 'notnull', 'version');
  162. foreach ($column as $key => $value) {
  163. if (in_array($key, $allowed)) {
  164. $fieldMapping[$key] = $value;
  165. }
  166. }
  167. $metadata->mapField($fieldMapping);
  168. if (isset($column['autoincrement'])) {
  169. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
  170. } else if (isset($column['sequence'])) {
  171. $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE);
  172. $definition = array(
  173. 'sequenceName' => is_array($column['sequence']) ? $column['sequence']['name']:$column['sequence']
  174. );
  175. if (isset($column['sequence']['size'])) {
  176. $definition['allocationSize'] = $column['sequence']['size'];
  177. }
  178. if (isset($column['sequence']['value'])) {
  179. $definition['initialValue'] = $column['sequence']['value'];
  180. }
  181. $metadata->setSequenceGeneratorDefinition($definition);
  182. }
  183. return $fieldMapping;
  184. }
  185. private function _convertIndexes($className, array $model, ClassMetadataInfo $metadata)
  186. {
  187. if (isset($model['indexes']) && $model['indexes']) {
  188. foreach ($model['indexes'] as $name => $index) {
  189. $type = (isset($index['type']) && $index['type'] == 'unique')
  190. ? 'uniqueConstraints' : 'indexes';
  191. $metadata->table[$type][$name] = array(
  192. 'columns' => $index['fields']
  193. );
  194. }
  195. }
  196. }
  197. private function _convertRelations($className, array $model, ClassMetadataInfo $metadata)
  198. {
  199. if (isset($model['relations']) && $model['relations']) {
  200. foreach ($model['relations'] as $name => $relation) {
  201. if ( ! isset($relation['alias'])) {
  202. $relation['alias'] = $name;
  203. }
  204. if ( ! isset($relation['class'])) {
  205. $relation['class'] = $name;
  206. }
  207. if ( ! isset($relation['local'])) {
  208. $relation['local'] = Inflector::tableize($relation['class']);
  209. }
  210. if ( ! isset($relation['foreign'])) {
  211. $relation['foreign'] = 'id';
  212. }
  213. if ( ! isset($relation['foreignAlias'])) {
  214. $relation['foreignAlias'] = $className;
  215. }
  216. if (isset($relation['refClass'])) {
  217. $type = 'many';
  218. $foreignType = 'many';
  219. $joinColumns = array();
  220. } else {
  221. $type = isset($relation['type']) ? $relation['type'] : 'one';
  222. $foreignType = isset($relation['foreignType']) ? $relation['foreignType'] : 'many';
  223. $joinColumns = array(
  224. array(
  225. 'name' => $relation['local'],
  226. 'referencedColumnName' => $relation['foreign'],
  227. 'onDelete' => isset($relation['onDelete']) ? $relation['onDelete'] : null,
  228. 'onUpdate' => isset($relation['onUpdate']) ? $relation['onUpdate'] : null,
  229. )
  230. );
  231. }
  232. if ($type == 'one' && $foreignType == 'one') {
  233. $method = 'mapOneToOne';
  234. } else if ($type == 'many' && $foreignType == 'many') {
  235. $method = 'mapManyToMany';
  236. } else {
  237. $method = 'mapOneToMany';
  238. }
  239. $associationMapping = array();
  240. $associationMapping['fieldName'] = $relation['alias'];
  241. $associationMapping['targetEntity'] = $relation['class'];
  242. $associationMapping['mappedBy'] = $relation['foreignAlias'];
  243. $associationMapping['joinColumns'] = $joinColumns;
  244. $metadata->$method($associationMapping);
  245. }
  246. }
  247. }
  248. }