SingleTablePersister.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\Persisters;
  20. use Doctrine\ORM\Mapping\ClassMetadata;
  21. use Doctrine\Common\Collections\Criteria;
  22. /**
  23. * Persister for entities that participate in a hierarchy mapped with the
  24. * SINGLE_TABLE strategy.
  25. *
  26. * @author Roman Borschel <roman@code-factory.org>
  27. * @author Benjamin Eberlei <kontakt@beberlei.de>
  28. * @author Alexander <iam.asm89@gmail.com>
  29. * @since 2.0
  30. * @link http://martinfowler.com/eaaCatalog/singleTableInheritance.html
  31. */
  32. class SingleTablePersister extends AbstractEntityInheritancePersister
  33. {
  34. /** {@inheritdoc} */
  35. protected function _getDiscriminatorColumnTableName()
  36. {
  37. return $this->_class->getTableName();
  38. }
  39. /** {@inheritdoc} */
  40. protected function _getSelectColumnListSQL()
  41. {
  42. if ($this->_selectColumnListSql !== null) {
  43. return $this->_selectColumnListSql;
  44. }
  45. $columnList = parent::_getSelectColumnListSQL();
  46. $rootClass = $this->_em->getClassMetadata($this->_class->rootEntityName);
  47. $tableAlias = $this->_getSQLTableAlias($rootClass->name);
  48. // Append discriminator column
  49. $discrColumn = $this->_class->discriminatorColumn['name'];
  50. $columnList .= ', ' . $tableAlias . '.' . $discrColumn;
  51. $resultColumnName = $this->_platform->getSQLResultCasing($discrColumn);
  52. $this->_rsm->setDiscriminatorColumn('r', $resultColumnName);
  53. $this->_rsm->addMetaResult('r', $resultColumnName, $discrColumn);
  54. // Append subclass columns
  55. foreach ($this->_class->subClasses as $subClassName) {
  56. $subClass = $this->_em->getClassMetadata($subClassName);
  57. // Regular columns
  58. foreach ($subClass->fieldMappings as $fieldName => $mapping) {
  59. if ( ! isset($mapping['inherited'])) {
  60. $columnList .= ', ' . $this->_getSelectColumnSQL($fieldName, $subClass);
  61. }
  62. }
  63. // Foreign key columns
  64. foreach ($subClass->associationMappings as $assoc) {
  65. if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE && ! isset($assoc['inherited'])) {
  66. foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
  67. if ($columnList != '') $columnList .= ', ';
  68. $columnList .= $this->getSelectJoinColumnSQL(
  69. $tableAlias,
  70. $srcColumn,
  71. isset($assoc['inherited']) ? $assoc['inherited'] : $this->_class->name
  72. );
  73. }
  74. }
  75. }
  76. }
  77. $this->_selectColumnListSql = $columnList;
  78. return $this->_selectColumnListSql;
  79. }
  80. /** {@inheritdoc} */
  81. protected function _getInsertColumnList()
  82. {
  83. $columns = parent::_getInsertColumnList();
  84. // Add discriminator column to the INSERT SQL
  85. $columns[] = $this->_class->discriminatorColumn['name'];
  86. return $columns;
  87. }
  88. /** {@inheritdoc} */
  89. protected function _getSQLTableAlias($className, $assocName = '')
  90. {
  91. return parent::_getSQLTableAlias($this->_class->rootEntityName, $assocName);
  92. }
  93. /** {@inheritdoc} */
  94. protected function _getSelectConditionSQL(array $criteria, $assoc = null)
  95. {
  96. $conditionSql = parent::_getSelectConditionSQL($criteria, $assoc);
  97. if ($conditionSql) {
  98. $conditionSql .= ' AND ';
  99. }
  100. return $conditionSql . $this->_getSelectConditionDiscriminatorValueSQL();
  101. }
  102. /** {@inheritdoc} */
  103. protected function _getSelectConditionCriteriaSQL(Criteria $criteria)
  104. {
  105. $conditionSql = parent::_getSelectConditionCriteriaSQL($criteria);
  106. if ($conditionSql) {
  107. $conditionSql .= ' AND ';
  108. }
  109. return $conditionSql . $this->_getSelectConditionDiscriminatorValueSQL();
  110. }
  111. protected function _getSelectConditionDiscriminatorValueSQL()
  112. {
  113. $values = array();
  114. if ($this->_class->discriminatorValue !== null) { // discriminators can be 0
  115. $values[] = $this->_conn->quote($this->_class->discriminatorValue);
  116. }
  117. $discrValues = array_flip($this->_class->discriminatorMap);
  118. foreach ($this->_class->subClasses as $subclassName) {
  119. $values[] = $this->_conn->quote($discrValues[$subclassName]);
  120. }
  121. return $this->_getSQLTableAlias($this->_class->name) . '.' . $this->_class->discriminatorColumn['name']
  122. . ' IN (' . implode(', ', $values) . ')';
  123. }
  124. /** {@inheritdoc} */
  125. protected function generateFilterConditionSQL(ClassMetadata $targetEntity, $targetTableAlias)
  126. {
  127. // Ensure that the filters are applied to the root entity of the inheritance tree
  128. $targetEntity = $this->_em->getClassMetadata($targetEntity->rootEntityName);
  129. // we dont care about the $targetTableAlias, in a STI there is only one table.
  130. return parent::generateFilterConditionSQL($targetEntity, $targetTableAlias);
  131. }
  132. }