DefaultNamingStrategy.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Mapping;
  20. use function strpos;
  21. use function strrpos;
  22. use function strtolower;
  23. use function substr;
  24. /**
  25. * The default NamingStrategy
  26. *
  27. * @link www.doctrine-project.org
  28. */
  29. class DefaultNamingStrategy implements NamingStrategy
  30. {
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function classToTableName($className)
  35. {
  36. if (strpos($className, '\\') !== false) {
  37. return substr($className, strrpos($className, '\\') + 1);
  38. }
  39. return $className;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function propertyToColumnName($propertyName, $className = null)
  45. {
  46. return $propertyName;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
  52. {
  53. return $propertyName . '_' . $embeddedColumnName;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function referenceColumnName()
  59. {
  60. return 'id';
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function joinColumnName($propertyName, $className = null)
  66. {
  67. return $propertyName . '_' . $this->referenceColumnName();
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
  73. {
  74. return strtolower($this->classToTableName($sourceEntity) . '_' .
  75. $this->classToTableName($targetEntity));
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function joinKeyColumnName($entityName, $referencedColumnName = null)
  81. {
  82. return strtolower($this->classToTableName($entityName) . '_' .
  83. ($referencedColumnName ?: $this->referenceColumnName()));
  84. }
  85. }