namingstrategy.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. Implementing a NamingStrategy
  2. ==============================
  3. .. versionadded:: 2.3
  4. Using a naming strategy you can provide rules for automatically generating
  5. database identifiers, columns and tables names
  6. when the table/column name is not given.
  7. This feature helps reduce the verbosity of the mapping document,
  8. eliminating repetitive noise (eg: ``TABLE_``).
  9. Configuring a naming strategy
  10. -----------------------------
  11. The default strategy used by Doctrine is quite minimal.
  12. By default the ``Doctrine\ORM\Mapping\DefaultNamingStrategy``
  13. uses the simple class name and the attributes names to generate tables and columns
  14. You can specify a different strategy by calling ``Doctrine\ORM\Configuration#setNamingStrategy()`` :
  15. .. code-block:: php
  16. <?php
  17. $namingStrategy = new MyNamingStrategy();
  18. $configuration()->setNamingStrategy($namingStrategy);
  19. Underscore naming strategy
  20. ---------------------------
  21. ``\Doctrine\ORM\Mapping\UnderscoreNamingStrategy`` is a built-in strategy
  22. that might be a useful if you want to use a underlying convention.
  23. .. code-block:: php
  24. <?php
  25. $namingStrategy = new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy(CASE_UPPER);
  26. $configuration()->setNamingStrategy($namingStrategy);
  27. Then SomeEntityName will generate the table SOME_ENTITY_NAME when CASE_UPPER
  28. or some_entity_name using CASE_LOWER is given.
  29. Naming strategy interface
  30. -------------------------
  31. The interface ``Doctrine\ORM\Mapping\NamingStrategy`` allows you to specify
  32. a "naming standard" for database tables and columns.
  33. .. code-block:: php
  34. <?php
  35. /**
  36. * Return a table name for an entity class
  37. *
  38. * @param string $className The fully-qualified class name
  39. * @return string A table name
  40. */
  41. function classToTableName($className);
  42. /**
  43. * Return a column name for a property
  44. *
  45. * @param string $propertyName A property
  46. * @return string A column name
  47. */
  48. function propertyToColumnName($propertyName);
  49. /**
  50. * Return the default reference column name
  51. *
  52. * @return string A column name
  53. */
  54. function referenceColumnName();
  55. /**
  56. * Return a join column name for a property
  57. *
  58. * @param string $propertyName A property
  59. * @return string A join column name
  60. */
  61. function joinColumnName($propertyName);
  62. /**
  63. * Return a join table name
  64. *
  65. * @param string $sourceEntity The source entity
  66. * @param string $targetEntity The target entity
  67. * @param string $propertyName A property
  68. * @return string A join table name
  69. */
  70. function joinTableName($sourceEntity, $targetEntity, $propertyName = null);
  71. /**
  72. * Return the foreign key column name for the given parameters
  73. *
  74. * @param string $entityName A entity
  75. * @param string $referencedColumnName A property
  76. * @return string A join column name
  77. */
  78. function joinKeyColumnName($entityName, $referencedColumnName = null);
  79. Implementing a naming strategy
  80. -------------------------------
  81. If you have database naming standards like all tables names should be prefixed
  82. by the application prefix, all column names should be upper case,
  83. you can easily achieve such standards by implementing a naming strategy.
  84. You need to implements NamingStrategy first. Following is an example
  85. .. code-block:: php
  86. <?php
  87. class MyAppNamingStrategy implements NamingStrategy
  88. {
  89. public function classToTableName($className)
  90. {
  91. return 'MyApp_' . substr($className, strrpos($className, '\\') + 1);
  92. }
  93. public function propertyToColumnName($propertyName)
  94. {
  95. return $propertyName;
  96. }
  97. public function referenceColumnName()
  98. {
  99. return 'id';
  100. }
  101. public function joinColumnName($propertyName)
  102. {
  103. return $propertyName . '_' . $this->referenceColumnName();
  104. }
  105. public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
  106. {
  107. return strtolower($this->classToTableName($sourceEntity) . '_' .
  108. $this->classToTableName($targetEntity));
  109. }
  110. public function joinKeyColumnName($entityName, $referencedColumnName = null)
  111. {
  112. return strtolower($this->classToTableName($entityName) . '_' .
  113. ($referencedColumnName ?: $this->referenceColumnName()));
  114. }
  115. }
  116. Configuring the namingstrategy is easy if.
  117. Just set your naming strategy calling ``Doctrine\ORM\Configuration#setNamingStrategy()`` :.
  118. .. code-block:: php
  119. <?php
  120. $namingStrategy = new MyAppNamingStrategy();
  121. $configuration()->setNamingStrategy($namingStrategy);