yaml_exporter.class.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 Tools;
  22. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  23. use Doctrine\ORM\Tools\Export\Driver\AbstractExporter;
  24. /**
  25. * ClassMetadata exporter for Doctrine YAML mapping files
  26. *
  27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  28. * @link www.doctrine-project.org
  29. * @since 2.0
  30. * @version $Revision$
  31. * @author Jonathan Wage <jonwage@gmail.com>
  32. */
  33. class YamlExporter extends AbstractExporter
  34. {
  35. protected $_extension = '.dcm.yml';
  36. /**
  37. * Converts a single ClassMetadata instance to the exported format
  38. * and returns it
  39. *
  40. * TODO: Should this code be pulled out in to a toArray() method in ClassMetadata
  41. *
  42. * @param ClassMetadataInfo $metadata
  43. * @return mixed $exported
  44. */
  45. public function exportClassMetadata(ClassMetadataInfo $metadata)
  46. {
  47. $array = array();
  48. if ($metadata->isMappedSuperclass) {
  49. $array['type'] = 'mappedSuperclass';
  50. } else {
  51. $array['type'] = 'entity';
  52. }
  53. $array['table'] = $metadata->table['name'];
  54. if (isset($metadata->table['schema'])) {
  55. $array['schema'] = $metadata->table['schema'];
  56. }
  57. $inheritanceType = $metadata->inheritanceType;
  58. if ($inheritanceType !== ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
  59. $array['inheritanceType'] = $this->_getInheritanceTypeString($inheritanceType);
  60. }
  61. if ($column = $metadata->discriminatorColumn) {
  62. $array['discriminatorColumn'] = $column;
  63. }
  64. if ($map = $metadata->discriminatorMap) {
  65. $array['discriminatorMap'] = $map;
  66. }
  67. if ($metadata->changeTrackingPolicy !== ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT) {
  68. $array['changeTrackingPolicy'] = $this->_getChangeTrackingPolicyString($metadata->changeTrackingPolicy);
  69. }
  70. if (isset($metadata->table['indexes'])) {
  71. $array['indexes'] = $metadata->table['indexes'];
  72. }
  73. if ($metadata->customRepositoryClassName) {
  74. $array['repositoryClass'] = $metadata->customRepositoryClassName;
  75. }
  76. if (isset($metadata->table['uniqueConstraints'])) {
  77. $array['uniqueConstraints'] = $metadata->table['uniqueConstraints'];
  78. }
  79. $fieldMappings = $metadata->fieldMappings;
  80. $ids = array();
  81. foreach ($fieldMappings as $name => $fieldMapping) {
  82. $fieldMapping['column'] = $fieldMapping['columnName'];
  83. unset(
  84. $fieldMapping['columnName'],
  85. $fieldMapping['fieldName']
  86. );
  87. if ($fieldMapping['column'] == $name) {
  88. unset($fieldMapping['column']);
  89. }
  90. if (isset($fieldMapping['id']) && $fieldMapping['id']) {
  91. $ids[$name] = $fieldMapping;
  92. unset($fieldMappings[$name]);
  93. continue;
  94. }
  95. $fieldMappings[$name] = $fieldMapping;
  96. }
  97. if ($idGeneratorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) {
  98. $ids[$metadata->getSingleIdentifierFieldName()]['generator']['strategy'] = $this->_getIdGeneratorTypeString($metadata->generatorType);
  99. }else{
  100. if(count($metadata->identifier) == 2){
  101. foreach($metadata->identifier as $identifier){
  102. if($identifier != 'c_id'){
  103. $ids[$identifier]['generator']['strategy'] = 'IDENTITY';
  104. }
  105. }
  106. }
  107. }
  108. if ($ids) {
  109. $array['fields'] = $ids;
  110. }
  111. if ($fieldMappings) {
  112. if ( ! isset($array['fields'])) {
  113. $array['fields'] = array();
  114. }
  115. $array['fields'] = array_merge($array['fields'], $fieldMappings);
  116. }
  117. $associations = array();
  118. foreach ($metadata->associationMappings as $name => $associationMapping) {
  119. $cascade = array();
  120. if ($associationMapping['isCascadeRemove']) {
  121. $cascade[] = 'remove';
  122. }
  123. if ($associationMapping['isCascadePersist']) {
  124. $cascade[] = 'persist';
  125. }
  126. if ($associationMapping['isCascadeRefresh']) {
  127. $cascade[] = 'refresh';
  128. }
  129. if ($associationMapping['isCascadeMerge']) {
  130. $cascade[] = 'merge';
  131. }
  132. if ($associationMapping['isCascadeDetach']) {
  133. $cascade[] = 'detach';
  134. }
  135. if (count($cascade) === 5) {
  136. $cascade = array('all');
  137. }
  138. $associationMappingArray = array(
  139. 'targetEntity' => $associationMapping['targetEntity'],
  140. 'cascade' => $cascade,
  141. );
  142. if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) {
  143. $joinColumns = $associationMapping['joinColumns'];
  144. $newJoinColumns = array();
  145. foreach ($joinColumns as $joinColumn) {
  146. $newJoinColumns[$joinColumn['name']]['referencedColumnName'] = $joinColumn['referencedColumnName'];
  147. if (isset($joinColumn['onDelete'])) {
  148. $newJoinColumns[$joinColumn['name']]['onDelete'] = $joinColumn['onDelete'];
  149. }
  150. }
  151. $oneToOneMappingArray = array(
  152. 'mappedBy' => $associationMapping['mappedBy'],
  153. 'inversedBy' => $associationMapping['inversedBy'],
  154. 'joinColumns' => $newJoinColumns,
  155. 'orphanRemoval' => $associationMapping['orphanRemoval'],
  156. );
  157. $associationMappingArray = array_merge($associationMappingArray, $oneToOneMappingArray);
  158. $array['oneToOne'][$name] = $associationMappingArray;
  159. } else if ($associationMapping['type'] == ClassMetadataInfo::ONE_TO_MANY) {
  160. $oneToManyMappingArray = array(
  161. 'mappedBy' => $associationMapping['mappedBy'],
  162. 'inversedBy' => $associationMapping['inversedBy'],
  163. 'orphanRemoval' => $associationMapping['orphanRemoval'],
  164. 'orderBy' => isset($associationMapping['orderBy']) ? $associationMapping['orderBy'] : null
  165. );
  166. $associationMappingArray = array_merge($associationMappingArray, $oneToManyMappingArray);
  167. $array['oneToMany'][$name] = $associationMappingArray;
  168. } else if ($associationMapping['type'] == ClassMetadataInfo::MANY_TO_MANY) {
  169. $manyToManyMappingArray = array(
  170. 'mappedBy' => $associationMapping['mappedBy'],
  171. 'inversedBy' => $associationMapping['inversedBy'],
  172. 'joinTable' => isset($associationMapping['joinTable']) ? $associationMapping['joinTable'] : null,
  173. 'orderBy' => isset($associationMapping['orderBy']) ? $associationMapping['orderBy'] : null
  174. );
  175. $associationMappingArray = array_merge($associationMappingArray, $manyToManyMappingArray);
  176. $array['manyToMany'][$name] = $associationMappingArray;
  177. }
  178. }
  179. if (isset($metadata->lifecycleCallbacks)) {
  180. $array['lifecycleCallbacks'] = $metadata->lifecycleCallbacks;
  181. }
  182. return \Symfony\Component\Yaml\Yaml::dump(array($metadata->name => $array), 10);
  183. }
  184. }