AbstractEntityInheritancePersister.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\DBAL\Types\Type;
  22. /**
  23. * Base class for entity persisters that implement a certain inheritance mapping strategy.
  24. * All these persisters are assumed to use a discriminator column to discriminate entity
  25. * types in the hierarchy.
  26. *
  27. * @author Roman Borschel <roman@code-factory.org>
  28. * @author Benjamin Eberlei <kontakt@beberlei.de>
  29. * @since 2.0
  30. */
  31. abstract class AbstractEntityInheritancePersister extends BasicEntityPersister
  32. {
  33. /**
  34. * {@inheritdoc}
  35. */
  36. protected function prepareInsertData($entity)
  37. {
  38. $data = parent::prepareInsertData($entity);
  39. // Populate the discriminator column
  40. $discColumn = $this->class->discriminatorColumn;
  41. $this->columnTypes[$discColumn['name']] = $discColumn['type'];
  42. $data[$this->getDiscriminatorColumnTableName()][$discColumn['name']] = $this->class->discriminatorValue;
  43. return $data;
  44. }
  45. /**
  46. * Gets the name of the table that contains the discriminator column.
  47. *
  48. * @return string The table name.
  49. */
  50. abstract protected function getDiscriminatorColumnTableName();
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected function getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r')
  55. {
  56. $tableAlias = $alias == 'r' ? '' : $alias;
  57. $columnName = $class->columnNames[$field];
  58. $columnAlias = $this->getSQLColumnAlias($columnName);
  59. $sql = $this->getSQLTableAlias($class->name, $tableAlias) . '.'
  60. . $this->quoteStrategy->getColumnName($field, $class, $this->platform);
  61. $this->rsm->addFieldResult($alias, $columnAlias, $field, $class->name);
  62. if (isset($class->fieldMappings[$field]['requireSQLConversion'])) {
  63. $type = Type::getType($class->getTypeOfField($field));
  64. $sql = $type->convertToPHPValueSQL($sql, $this->platform);
  65. }
  66. return $sql . ' AS ' . $columnAlias;
  67. }
  68. /**
  69. * @param string $tableAlias
  70. * @param string $joinColumnName
  71. * @param string $className
  72. *
  73. * @return string
  74. */
  75. protected function getSelectJoinColumnSQL($tableAlias, $joinColumnName, $className)
  76. {
  77. $columnAlias = $this->getSQLColumnAlias($joinColumnName);
  78. $this->rsm->addMetaResult('r', $columnAlias, $joinColumnName);
  79. return $tableAlias . '.' . $joinColumnName . ' AS ' . $columnAlias;
  80. }
  81. }