AbstractEntityInheritancePersister.php 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. 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. $columnName = $class->columnNames[$field];
  57. $sql = $this->_getSQLTableAlias($class->name, $alias == 'r' ? '' : $alias) . '.' . $class->getQuotedColumnName($field, $this->_platform);
  58. $columnAlias = $this->_platform->getSQLResultCasing($columnName . $this->_sqlAliasCounter++);
  59. $this->_rsm->addFieldResult($alias, $columnAlias, $field, $class->name);
  60. return "$sql AS $columnAlias";
  61. }
  62. protected function getSelectJoinColumnSQL($tableAlias, $joinColumnName, $className)
  63. {
  64. $columnAlias = $joinColumnName . $this->_sqlAliasCounter++;
  65. $resultColumnName = $this->_platform->getSQLResultCasing($columnAlias);
  66. $this->_rsm->addMetaResult('r', $resultColumnName, $joinColumnName);
  67. return $tableAlias . ".$joinColumnName AS $columnAlias";
  68. }
  69. }