XmlDriver.php 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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\Mapping\Driver;
  22. use SimpleXMLElement,
  23. Doctrine\ORM\Mapping\ClassMetadataInfo,
  24. Doctrine\ORM\Mapping\MappingException;
  25. /**
  26. * XmlDriver is a metadata driver that enables mapping through XML 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 Benjamin Eberlei <kontakt@beberlei.de>
  33. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  34. * @author Jonathan H. Wage <jonwage@gmail.com>
  35. * @author Roman Borschel <roman@code-factory.org>
  36. */
  37. class XmlDriver extends AbstractFileDriver
  38. {
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected $_fileExtension = '.dcm.xml';
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
  47. {
  48. $xmlRoot = $this->getElement($className);
  49. if ($xmlRoot->getName() == 'entity') {
  50. $metadata->setCustomRepositoryClass(
  51. isset($xmlRoot['repository-class']) ? (string)$xmlRoot['repository-class'] : null
  52. );
  53. if (isset($xmlRoot['read-only']) && $xmlRoot['read-only'] == "true") {
  54. $metadata->markReadOnly();
  55. }
  56. } else if ($xmlRoot->getName() == 'mapped-superclass') {
  57. $metadata->isMappedSuperclass = true;
  58. } else {
  59. throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
  60. }
  61. // Evaluate <entity...> attributes
  62. $table = array();
  63. if (isset($xmlRoot['table'])) {
  64. $table['name'] = (string)$xmlRoot['table'];
  65. }
  66. $metadata->setPrimaryTable($table);
  67. // Evaluate named queries
  68. if (isset($xmlRoot['named-queries'])) {
  69. foreach ($xmlRoot->{'named-queries'}->{'named-query'} as $namedQueryElement) {
  70. $metadata->addNamedQuery(array(
  71. 'name' => (string)$namedQueryElement['name'],
  72. 'query' => (string)$namedQueryElement['query']
  73. ));
  74. }
  75. }
  76. /* not implemented specially anyway. use table = schema.table
  77. if (isset($xmlRoot['schema'])) {
  78. $metadata->table['schema'] = (string)$xmlRoot['schema'];
  79. }*/
  80. if (isset($xmlRoot['inheritance-type'])) {
  81. $inheritanceType = (string)$xmlRoot['inheritance-type'];
  82. $metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceType));
  83. if ($metadata->inheritanceType != \Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) {
  84. // Evaluate <discriminator-column...>
  85. if (isset($xmlRoot->{'discriminator-column'})) {
  86. $discrColumn = $xmlRoot->{'discriminator-column'};
  87. $metadata->setDiscriminatorColumn(array(
  88. 'name' => (string)$discrColumn['name'],
  89. 'type' => (string)$discrColumn['type'],
  90. 'length' => (string)$discrColumn['length']
  91. ));
  92. } else {
  93. $metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255));
  94. }
  95. // Evaluate <discriminator-map...>
  96. if (isset($xmlRoot->{'discriminator-map'})) {
  97. $map = array();
  98. foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} AS $discrMapElement) {
  99. $map[(string)$discrMapElement['value']] = (string)$discrMapElement['class'];
  100. }
  101. $metadata->setDiscriminatorMap($map);
  102. }
  103. }
  104. }
  105. // Evaluate <change-tracking-policy...>
  106. if (isset($xmlRoot['change-tracking-policy'])) {
  107. $metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_'
  108. . strtoupper((string)$xmlRoot['change-tracking-policy'])));
  109. }
  110. // Evaluate <indexes...>
  111. if (isset($xmlRoot->indexes)) {
  112. $metadata->table['indexes'] = array();
  113. foreach ($xmlRoot->indexes->index as $index) {
  114. $columns = explode(',', (string)$index['columns']);
  115. if (isset($index['name'])) {
  116. $metadata->table['indexes'][(string)$index['name']] = array(
  117. 'columns' => $columns
  118. );
  119. } else {
  120. $metadata->table['indexes'][] = array(
  121. 'columns' => $columns
  122. );
  123. }
  124. }
  125. }
  126. // Evaluate <unique-constraints..>
  127. if (isset($xmlRoot->{'unique-constraints'})) {
  128. $metadata->table['uniqueConstraints'] = array();
  129. foreach ($xmlRoot->{'unique-constraints'}->{'unique-constraint'} as $unique) {
  130. $columns = explode(',', (string)$unique['columns']);
  131. if (isset($unique['name'])) {
  132. $metadata->table['uniqueConstraints'][(string)$unique['name']] = array(
  133. 'columns' => $columns
  134. );
  135. } else {
  136. $metadata->table['uniqueConstraints'][] = array(
  137. 'columns' => $columns
  138. );
  139. }
  140. }
  141. }
  142. // Evaluate <field ...> mappings
  143. if (isset($xmlRoot->field)) {
  144. foreach ($xmlRoot->field as $fieldMapping) {
  145. $mapping = array(
  146. 'fieldName' => (string)$fieldMapping['name'],
  147. 'type' => (string)$fieldMapping['type']
  148. );
  149. if (isset($fieldMapping['column'])) {
  150. $mapping['columnName'] = (string)$fieldMapping['column'];
  151. }
  152. if (isset($fieldMapping['length'])) {
  153. $mapping['length'] = (int)$fieldMapping['length'];
  154. }
  155. if (isset($fieldMapping['precision'])) {
  156. $mapping['precision'] = (int)$fieldMapping['precision'];
  157. }
  158. if (isset($fieldMapping['scale'])) {
  159. $mapping['scale'] = (int)$fieldMapping['scale'];
  160. }
  161. if (isset($fieldMapping['unique'])) {
  162. $mapping['unique'] = ((string)$fieldMapping['unique'] == "false") ? false : true;
  163. }
  164. if (isset($fieldMapping['options'])) {
  165. $mapping['options'] = (array)$fieldMapping['options'];
  166. }
  167. if (isset($fieldMapping['nullable'])) {
  168. $mapping['nullable'] = ((string)$fieldMapping['nullable'] == "false") ? false : true;
  169. }
  170. if (isset($fieldMapping['version']) && $fieldMapping['version']) {
  171. $metadata->setVersionMapping($mapping);
  172. }
  173. if (isset($fieldMapping['column-definition'])) {
  174. $mapping['columnDefinition'] = (string)$fieldMapping['column-definition'];
  175. }
  176. $metadata->mapField($mapping);
  177. }
  178. }
  179. // Evaluate <id ...> mappings
  180. $associationIds = array();
  181. foreach ($xmlRoot->id as $idElement) {
  182. if ((bool)$idElement['association-key'] == true) {
  183. $associationIds[(string)$idElement['fieldName']] = true;
  184. continue;
  185. }
  186. $mapping = array(
  187. 'id' => true,
  188. 'fieldName' => (string)$idElement['name'],
  189. 'type' => (string)$idElement['type']
  190. );
  191. if (isset($idElement['column'])) {
  192. $mapping['columnName'] = (string)$idElement['column'];
  193. }
  194. $metadata->mapField($mapping);
  195. if (isset($idElement->generator)) {
  196. $strategy = isset($idElement->generator['strategy']) ?
  197. (string)$idElement->generator['strategy'] : 'AUTO';
  198. $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
  199. . $strategy));
  200. }
  201. // Check for SequenceGenerator/TableGenerator definition
  202. if (isset($idElement->{'sequence-generator'})) {
  203. $seqGenerator = $idElement->{'sequence-generator'};
  204. $metadata->setSequenceGeneratorDefinition(array(
  205. 'sequenceName' => (string)$seqGenerator['sequence-name'],
  206. 'allocationSize' => (string)$seqGenerator['allocation-size'],
  207. 'initialValue' => (string)$seqGenerator['initial-value']
  208. ));
  209. } else if (isset($idElement->{'table-generator'})) {
  210. throw MappingException::tableIdGeneratorNotImplemented($className);
  211. }
  212. }
  213. // Evaluate <one-to-one ...> mappings
  214. if (isset($xmlRoot->{'one-to-one'})) {
  215. foreach ($xmlRoot->{'one-to-one'} as $oneToOneElement) {
  216. $mapping = array(
  217. 'fieldName' => (string)$oneToOneElement['field'],
  218. 'targetEntity' => (string)$oneToOneElement['target-entity']
  219. );
  220. if (isset($associationIds[$mapping['fieldName']])) {
  221. $mapping['id'] = true;
  222. }
  223. if (isset($oneToOneElement['fetch'])) {
  224. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string)$oneToOneElement['fetch']);
  225. }
  226. if (isset($oneToOneElement['mapped-by'])) {
  227. $mapping['mappedBy'] = (string)$oneToOneElement['mapped-by'];
  228. } else {
  229. if (isset($oneToOneElement['inversed-by'])) {
  230. $mapping['inversedBy'] = (string)$oneToOneElement['inversed-by'];
  231. }
  232. $joinColumns = array();
  233. if (isset($oneToOneElement->{'join-column'})) {
  234. $joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement->{'join-column'});
  235. } else if (isset($oneToOneElement->{'join-columns'})) {
  236. foreach ($oneToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
  237. $joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
  238. }
  239. }
  240. $mapping['joinColumns'] = $joinColumns;
  241. }
  242. if (isset($oneToOneElement->cascade)) {
  243. $mapping['cascade'] = $this->_getCascadeMappings($oneToOneElement->cascade);
  244. }
  245. if (isset($oneToOneElement['orphan-removal'])) {
  246. $mapping['orphanRemoval'] = (bool)$oneToOneElement['orphan-removal'];
  247. }
  248. $metadata->mapOneToOne($mapping);
  249. }
  250. }
  251. // Evaluate <one-to-many ...> mappings
  252. if (isset($xmlRoot->{'one-to-many'})) {
  253. foreach ($xmlRoot->{'one-to-many'} as $oneToManyElement) {
  254. $mapping = array(
  255. 'fieldName' => (string)$oneToManyElement['field'],
  256. 'targetEntity' => (string)$oneToManyElement['target-entity'],
  257. 'mappedBy' => (string)$oneToManyElement['mapped-by']
  258. );
  259. if (isset($oneToManyElement['fetch'])) {
  260. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string)$oneToManyElement['fetch']);
  261. }
  262. if (isset($oneToManyElement->cascade)) {
  263. $mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement->cascade);
  264. }
  265. if (isset($oneToManyElement['orphan-removal'])) {
  266. $mapping['orphanRemoval'] = (bool)$oneToManyElement['orphan-removal'];
  267. }
  268. if (isset($oneToManyElement->{'order-by'})) {
  269. $orderBy = array();
  270. foreach ($oneToManyElement->{'order-by'}->{'order-by-field'} AS $orderByField) {
  271. $orderBy[(string)$orderByField['name']] = (string)$orderByField['direction'];
  272. }
  273. $mapping['orderBy'] = $orderBy;
  274. }
  275. if (isset($oneToManyElement->{'index-by'})) {
  276. $mapping['indexBy'] = (string)$oneToManyElement->{'index-by'};
  277. }
  278. $metadata->mapOneToMany($mapping);
  279. }
  280. }
  281. // Evaluate <many-to-one ...> mappings
  282. if (isset($xmlRoot->{'many-to-one'})) {
  283. foreach ($xmlRoot->{'many-to-one'} as $manyToOneElement) {
  284. $mapping = array(
  285. 'fieldName' => (string)$manyToOneElement['field'],
  286. 'targetEntity' => (string)$manyToOneElement['target-entity']
  287. );
  288. if (isset($associationIds[$mapping['fieldName']])) {
  289. $mapping['id'] = true;
  290. }
  291. if (isset($manyToOneElement['fetch'])) {
  292. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string)$manyToOneElement['fetch']);
  293. }
  294. if (isset($manyToOneElement['inversed-by'])) {
  295. $mapping['inversedBy'] = (string)$manyToOneElement['inversed-by'];
  296. }
  297. $joinColumns = array();
  298. if (isset($manyToOneElement->{'join-column'})) {
  299. $joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement->{'join-column'});
  300. } else if (isset($manyToOneElement->{'join-columns'})) {
  301. foreach ($manyToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
  302. $joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
  303. }
  304. }
  305. $mapping['joinColumns'] = $joinColumns;
  306. if (isset($manyToOneElement->cascade)) {
  307. $mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement->cascade);
  308. }
  309. if (isset($manyToOneElement->{'orphan-removal'})) {
  310. $mapping['orphanRemoval'] = (bool)$manyToOneElement->{'orphan-removal'};
  311. }
  312. $metadata->mapManyToOne($mapping);
  313. }
  314. }
  315. // Evaluate <many-to-many ...> mappings
  316. if (isset($xmlRoot->{'many-to-many'})) {
  317. foreach ($xmlRoot->{'many-to-many'} as $manyToManyElement) {
  318. $mapping = array(
  319. 'fieldName' => (string)$manyToManyElement['field'],
  320. 'targetEntity' => (string)$manyToManyElement['target-entity']
  321. );
  322. if (isset($manyToManyElement['fetch'])) {
  323. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string)$manyToManyElement['fetch']);
  324. }
  325. if (isset($manyToManyElement['mapped-by'])) {
  326. $mapping['mappedBy'] = (string)$manyToManyElement['mapped-by'];
  327. } else if (isset($manyToManyElement->{'join-table'})) {
  328. if (isset($manyToManyElement['inversed-by'])) {
  329. $mapping['inversedBy'] = (string)$manyToManyElement['inversed-by'];
  330. }
  331. $joinTableElement = $manyToManyElement->{'join-table'};
  332. $joinTable = array(
  333. 'name' => (string)$joinTableElement['name']
  334. );
  335. if (isset($joinTableElement['schema'])) {
  336. $joinTable['schema'] = (string)$joinTableElement['schema'];
  337. }
  338. foreach ($joinTableElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
  339. $joinTable['joinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
  340. }
  341. foreach ($joinTableElement->{'inverse-join-columns'}->{'join-column'} as $joinColumnElement) {
  342. $joinTable['inverseJoinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
  343. }
  344. $mapping['joinTable'] = $joinTable;
  345. }
  346. if (isset($manyToManyElement->cascade)) {
  347. $mapping['cascade'] = $this->_getCascadeMappings($manyToManyElement->cascade);
  348. }
  349. if (isset($manyToManyElement->{'orphan-removal'})) {
  350. $mapping['orphanRemoval'] = (bool)$manyToManyElement->{'orphan-removal'};
  351. }
  352. if (isset($manyToManyElement->{'order-by'})) {
  353. $orderBy = array();
  354. foreach ($manyToManyElement->{'order-by'}->{'order-by-field'} AS $orderByField) {
  355. $orderBy[(string)$orderByField['name']] = (string)$orderByField['direction'];
  356. }
  357. $mapping['orderBy'] = $orderBy;
  358. }
  359. if (isset($manyToManyElement->{'index-by'})) {
  360. $mapping['indexBy'] = (string)$manyToManyElement->{'index-by'};
  361. }
  362. $metadata->mapManyToMany($mapping);
  363. }
  364. }
  365. // Evaluate <lifecycle-callbacks...>
  366. if (isset($xmlRoot->{'lifecycle-callbacks'})) {
  367. foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) {
  368. $metadata->addLifecycleCallback((string)$lifecycleCallback['method'], constant('Doctrine\ORM\Events::' . (string)$lifecycleCallback['type']));
  369. }
  370. }
  371. }
  372. /**
  373. * Constructs a joinColumn mapping array based on the information
  374. * found in the given SimpleXMLElement.
  375. *
  376. * @param $joinColumnElement The XML element.
  377. * @return array The mapping array.
  378. */
  379. private function _getJoinColumnMapping(SimpleXMLElement $joinColumnElement)
  380. {
  381. $joinColumn = array(
  382. 'name' => (string)$joinColumnElement['name'],
  383. 'referencedColumnName' => (string)$joinColumnElement['referenced-column-name']
  384. );
  385. if (isset($joinColumnElement['unique'])) {
  386. $joinColumn['unique'] = ((string)$joinColumnElement['unique'] == "false") ? false : true;
  387. }
  388. if (isset($joinColumnElement['nullable'])) {
  389. $joinColumn['nullable'] = ((string)$joinColumnElement['nullable'] == "false") ? false : true;
  390. }
  391. if (isset($joinColumnElement['on-delete'])) {
  392. $joinColumn['onDelete'] = (string)$joinColumnElement['on-delete'];
  393. }
  394. if (isset($joinColumnElement['on-update'])) {
  395. $joinColumn['onUpdate'] = (string)$joinColumnElement['on-update'];
  396. }
  397. if (isset($joinColumnElement['column-definition'])) {
  398. $joinColumn['columnDefinition'] = (string)$joinColumnElement['column-definition'];
  399. }
  400. return $joinColumn;
  401. }
  402. /**
  403. * Gathers a list of cascade options found in the given cascade element.
  404. *
  405. * @param $cascadeElement The cascade element.
  406. * @return array The list of cascade options.
  407. */
  408. private function _getCascadeMappings($cascadeElement)
  409. {
  410. $cascades = array();
  411. foreach ($cascadeElement->children() as $action) {
  412. // According to the JPA specifications, XML uses "cascade-persist"
  413. // instead of "persist". Here, both variations
  414. // are supported because both YAML and Annotation use "persist"
  415. // and we want to make sure that this driver doesn't need to know
  416. // anything about the supported cascading actions
  417. $cascades[] = str_replace('cascade-', '', $action->getName());
  418. }
  419. return $cascades;
  420. }
  421. /**
  422. * {@inheritdoc}
  423. */
  424. protected function _loadMappingFile($file)
  425. {
  426. $result = array();
  427. $xmlElement = simplexml_load_file($file);
  428. if (isset($xmlElement->entity)) {
  429. foreach ($xmlElement->entity as $entityElement) {
  430. $entityName = (string)$entityElement['name'];
  431. $result[$entityName] = $entityElement;
  432. }
  433. } else if (isset($xmlElement->{'mapped-superclass'})) {
  434. foreach ($xmlElement->{'mapped-superclass'} as $mappedSuperClass) {
  435. $className = (string)$mappedSuperClass['name'];
  436. $result[$className] = $mappedSuperClass;
  437. }
  438. }
  439. return $result;
  440. }
  441. }