SingleTablePersister.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. $columnList = parent::_getSelectColumnListSQL();
  41. // Append discriminator column
  42. $discrColumn = $this->_class->discriminatorColumn['name'];
  43. $columnList .= ", $discrColumn";
  44. $rootClass = $this->_em->getClassMetadata($this->_class->rootEntityName);
  45. $tableAlias = $this->_getSQLTableAlias($rootClass->name);
  46. $resultColumnName = $this->_platform->getSQLResultCasing($discrColumn);
  47. $this->_rsm->setDiscriminatorColumn('r', $resultColumnName);
  48. $this->_rsm->addMetaResult('r', $resultColumnName, $discrColumn);
  49. // Append subclass columns
  50. foreach ($this->_class->subClasses as $subClassName) {
  51. $subClass = $this->_em->getClassMetadata($subClassName);
  52. // Regular columns
  53. foreach ($subClass->fieldMappings as $fieldName => $mapping) {
  54. if ( ! isset($mapping['inherited'])) {
  55. $columnList .= ', ' . $this->_getSelectColumnSQL($fieldName, $subClass);
  56. }
  57. }
  58. // Foreign key columns
  59. foreach ($subClass->associationMappings as $assoc) {
  60. if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE && ! isset($assoc['inherited'])) {
  61. foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) {
  62. if ($columnList != '') $columnList .= ', ';
  63. $columnList .= $this->getSelectJoinColumnSQL($tableAlias, $srcColumn,
  64. isset($assoc['inherited']) ? $assoc['inherited'] : $this->_class->name
  65. );
  66. }
  67. }
  68. }
  69. }
  70. return $columnList;
  71. }
  72. /** {@inheritdoc} */
  73. protected function _getInsertColumnList()
  74. {
  75. $columns = parent::_getInsertColumnList();
  76. // Add discriminator column to the INSERT SQL
  77. $columns[] = $this->_class->discriminatorColumn['name'];
  78. return $columns;
  79. }
  80. /** {@inheritdoc} */
  81. protected function _getSQLTableAlias($className, $assocName = '')
  82. {
  83. return parent::_getSQLTableAlias($this->_class->rootEntityName, $assocName);
  84. }
  85. /** {@inheritdoc} */
  86. protected function _getSelectConditionSQL(array $criteria, $assoc = null)
  87. {
  88. $conditionSql = parent::_getSelectConditionSQL($criteria, $assoc);
  89. // Append discriminator condition
  90. if ($conditionSql) $conditionSql .= ' AND ';
  91. $values = array();
  92. if ($this->_class->discriminatorValue !== null) { // discriminators can be 0
  93. $values[] = $this->_conn->quote($this->_class->discriminatorValue);
  94. }
  95. $discrValues = array_flip($this->_class->discriminatorMap);
  96. foreach ($this->_class->subClasses as $subclassName) {
  97. $values[] = $this->_conn->quote($discrValues[$subclassName]);
  98. }
  99. $conditionSql .= $this->_getSQLTableAlias($this->_class->name) . '.'
  100. . $this->_class->discriminatorColumn['name']
  101. . ' IN (' . implode(', ', $values) . ')';
  102. return $conditionSql;
  103. }
  104. }