SingleTablePersister.php 5.4 KB

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