SingleTablePersister.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Persisters;
  20. use Doctrine\ORM\Mapping\ClassMetadata;
  21. /**
  22. * Persister for entities that participate in a hierarchy mapped with the
  23. * SINGLE_TABLE strategy.
  24. *
  25. * @author Roman Borschel <roman@code-factory.org>
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. * @since 2.0
  28. * @link http://martinfowler.com/eaaCatalog/singleTableInheritance.html
  29. */
  30. class SingleTablePersister extends AbstractEntityInheritancePersister
  31. {
  32. /** {@inheritdoc} */
  33. protected function _getDiscriminatorColumnTableName()
  34. {
  35. return $this->_class->table['name'];
  36. }
  37. /** {@inheritdoc} */
  38. protected function _getSelectColumnListSQL()
  39. {
  40. if ($this->_selectColumnListSql !== null) {
  41. return $this->_selectColumnListSql;
  42. }
  43. $columnList = parent::_getSelectColumnListSQL();
  44. // Append discriminator column
  45. $discrColumn = $this->_class->discriminatorColumn['name'];
  46. $columnList .= ", $discrColumn";
  47. $rootClass = $this->_em->getClassMetadata($this->_class->rootEntityName);
  48. $tableAlias = $this->_getSQLTableAlias($rootClass->name);
  49. $resultColumnName = $this->_platform->getSQLResultCasing($discrColumn);
  50. $this->_rsm->setDiscriminatorColumn('r', $resultColumnName);
  51. $this->_rsm->addMetaResult('r', $resultColumnName, $discrColumn);
  52. // Append subclass columns
  53. foreach ($this->_class->subClasses as $subClassName) {
  54. $subClass = $this->_em->getClassMetadata($subClassName);
  55. // Regular columns
  56. foreach ($subClass->fieldMappings as $fieldName => $mapping) {
  57. if ( ! isset($mapping['inherited'])) {
  58. $columnList .= ', ' . $this->_getSelectColumnSQL($fieldName, $subClass);
  59. }
  60. }
  61. // Foreign key columns
  62. foreach ($subClass->associationMappings as $assoc) {
  63. if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE && ! isset($assoc['inherited'])) {
  64. foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
  65. if ($columnList != '') $columnList .= ', ';
  66. $columnList .= $this->getSelectJoinColumnSQL($tableAlias, $srcColumn,
  67. isset($assoc['inherited']) ? $assoc['inherited'] : $this->_class->name
  68. );
  69. }
  70. }
  71. }
  72. }
  73. $this->_selectColumnListSql = $columnList;
  74. return $this->_selectColumnListSql;
  75. }
  76. /** {@inheritdoc} */
  77. protected function _getInsertColumnList()
  78. {
  79. $columns = parent::_getInsertColumnList();
  80. // Add discriminator column to the INSERT SQL
  81. $columns[] = $this->_class->discriminatorColumn['name'];
  82. return $columns;
  83. }
  84. /** {@inheritdoc} */
  85. protected function _getSQLTableAlias($className, $assocName = '')
  86. {
  87. return parent::_getSQLTableAlias($this->_class->rootEntityName, $assocName);
  88. }
  89. /** {@inheritdoc} */
  90. protected function _getSelectConditionSQL(array $criteria, $assoc = null)
  91. {
  92. $conditionSql = parent::_getSelectConditionSQL($criteria, $assoc);
  93. // Append discriminator condition
  94. if ($conditionSql) $conditionSql .= ' AND ';
  95. $values = array();
  96. if ($this->_class->discriminatorValue !== null) { // discriminators can be 0
  97. $values[] = $this->_conn->quote($this->_class->discriminatorValue);
  98. }
  99. $discrValues = array_flip($this->_class->discriminatorMap);
  100. foreach ($this->_class->subClasses as $subclassName) {
  101. $values[] = $this->_conn->quote($discrValues[$subclassName]);
  102. }
  103. $conditionSql .= $this->_getSQLTableAlias($this->_class->name) . '.'
  104. . $this->_class->discriminatorColumn['name']
  105. . ' IN (' . implode(', ', $values) . ')';
  106. return $conditionSql;
  107. }
  108. }