yaml-mapping.rst 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. YAML Mapping
  2. ============
  3. The YAML mapping driver enables you to provide the ORM metadata in
  4. form of YAML documents.
  5. The YAML mapping document of a class is loaded on-demand the first
  6. time it is requested and subsequently stored in the metadata cache.
  7. In order to work, this requires certain conventions:
  8. - Each entity/mapped superclass must get its own dedicated YAML
  9. mapping document.
  10. - The name of the mapping document must consist of the fully
  11. qualified name of the class, where namespace separators are
  12. replaced by dots (.).
  13. - All mapping documents should get the extension ".dcm.yml" to
  14. identify it as a Doctrine mapping file. This is more of a
  15. convention and you are not forced to do this. You can change the
  16. file extension easily enough.
  17. .. code-block:: php
  18. <?php
  19. $driver->setFileExtension('.yml');
  20. It is recommended to put all YAML mapping documents in a single
  21. folder but you can spread the documents over several folders if you
  22. want to. In order to tell the YamlDriver where to look for your
  23. mapping documents, supply an array of paths as the first argument
  24. of the constructor, like this:
  25. .. code-block:: php
  26. <?php
  27. use Doctrine\ORM\Mapping\Driver\YamlDriver;
  28. // $config instanceof Doctrine\ORM\Configuration
  29. $driver = new YamlDriver(array('/path/to/files'));
  30. $config->setMetadataDriverImpl($driver);
  31. Simplified YAML Driver
  32. ~~~~~~~~~~~~~~~~~~~~~~
  33. The Symfony project sponsored a driver that simplifies usage of the YAML Driver.
  34. The changes between the original driver are:
  35. - File Extension is .orm.yml
  36. - Filenames are shortened, "MyProject\\Entities\\User" will become User.orm.yml
  37. - You can add a global file and add multiple entities in this file.
  38. Configuration of this client works a little bit different:
  39. .. code-block:: php
  40. <?php
  41. $namespaces = array(
  42. '/path/to/files1' => 'MyProject\Entities',
  43. '/path/to/files2' => 'OtherProject\Entities'
  44. );
  45. $driver = new \Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver($namespaces);
  46. $driver->setGlobalBasename('global'); // global.orm.yml
  47. Example
  48. -------
  49. As a quick start, here is a small example document that makes use
  50. of several common elements:
  51. .. code-block:: yaml
  52. # Doctrine.Tests.ORM.Mapping.User.dcm.yml
  53. Doctrine\Tests\ORM\Mapping\User:
  54. type: entity
  55. table: cms_users
  56. indexes:
  57. name_index:
  58. columns: [ name ]
  59. id:
  60. id:
  61. type: integer
  62. generator:
  63. strategy: AUTO
  64. fields:
  65. name:
  66. type: string
  67. length: 50
  68. oneToOne:
  69. address:
  70. targetEntity: Address
  71. joinColumn:
  72. name: address_id
  73. referencedColumnName: id
  74. oneToMany:
  75. phonenumbers:
  76. targetEntity: Phonenumber
  77. mappedBy: user
  78. cascade: ["persist", "merge"]
  79. manyToMany:
  80. groups:
  81. targetEntity: Group
  82. joinTable:
  83. name: cms_users_groups
  84. joinColumns:
  85. user_id:
  86. referencedColumnName: id
  87. inverseJoinColumns:
  88. group_id:
  89. referencedColumnName: id
  90. lifecycleCallbacks:
  91. prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersistToo ]
  92. postPersist: [ doStuffOnPostPersist ]
  93. Be aware that class-names specified in the YAML files should be
  94. fully qualified.