Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Hydrator hotfix - time to think about keeping types for columns #347

Closed
wants to merge 1 commit into from

3 participants

@rivaros

Line 251 - nobody cared to call convertToPHPValue($value, $this->_platform);

And I know why - it was hard to determine column type, easier to write $value. :)

... Because type is not in ResultSetMapping

... Because type is not in ClassMetadataInfo either

... Because column types are never considered in associations.

@travisbot

This pull request fails (merged 6fe926f into 4ef552e).

@rivaros

This hotfix is not for merging. It is more to show that we need a "type" attribute in JoinColumn.

@beberlei
Owner

This is not helpful, we know of this problem, but that is not the fix for it.

@beberlei beberlei closed this
@rivaros
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 32 additions and 2 deletions.
  1. +32 −2 lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
View
34 lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
@@ -218,7 +218,20 @@ protected function gatherRowData(array $data, array &$cache, array &$id, array &
// Meta column (has meaning in relational schema only, i.e. foreign keys or discriminator columns).
$fieldName = $this->_rsm->metaMappings[$key];
$classMetadata = $this->_em->getClassMetadata($this->_rsm->aliasMap[$this->_rsm->columnOwnerMap[$key]]);
-
+ //First try to determine type by searching fieldmappings
+ if (isset($classMetadata->fieldNames[$fieldName]) && isset($classMetadata->fieldMappings[$classMetadata->fieldNames[$fieldName]]['type'])) {
+ $cache[$key]['type'] = Type::getType($classMetadata->fieldMappings[$classMetadata->fieldNames[$fieldName]]['type']);
+ } else {
+ //if there is no type information in Entity we have no choice but to look into linked entities
+ foreach ($classMetadata->associationMappings as $assoc) {
+ foreach ($assoc['joinColumns'] as $joinColumn) {
+ if ($joinColumn['name'] == $fieldName) {
+ $cache[$key]['type'] = Type::getType($this->determineAssocColumnType($joinColumn['referencedColumnName'],$assoc['targetEntity']));
+ break 2;
+ }
+ }
+ }
+ }
$cache[$key]['isMetaColumn'] = true;
$cache[$key]['fieldName'] = $fieldName;
$cache[$key]['dqlAlias'] = $this->_rsm->columnOwnerMap[$key];
@@ -248,7 +261,7 @@ protected function gatherRowData(array $data, array &$cache, array &$id, array &
if (isset($cache[$key]['isMetaColumn'])) {
if ( ! isset($rowData[$dqlAlias][$cache[$key]['fieldName']]) && $value !== null) {
- $rowData[$dqlAlias][$cache[$key]['fieldName']] = $value;
+ $rowData[$dqlAlias][$cache[$key]['fieldName']] = isset ($cache[$key]['type']) ? $cache[$key]['type']->convertToPHPValue($value, $this->_platform) : $value;
if ($cache[$key]['isIdentifier']) {
$nonemptyComponents[$dqlAlias] = true;
}
@@ -275,6 +288,23 @@ protected function gatherRowData(array $data, array &$cache, array &$id, array &
}
/**
+ * Try to determine column type by analizing targetEntity field mappings
+ *
+ * @param string $columnName
+ * @param string $targetEntityClass
+ */
+ private function determineAssocColumnType($columnName, $targetEntityClass)
+ {
+ $targetEntity = $this->_em->getClassMetadata($targetEntityClass);
+ $targetField = $targetEntity->fieldNames[$columnName];
+ if ( isset ($targetField)) {
+ return $targetEntity->fieldMappings[$targetField]['type'];
+ } else {
+ return 'string';
+ }
+ }
+
+ /**
* Processes a row of the result set.
*
* Used for HYDRATE_SCALAR. This is a variant of _gatherRowData() that
Something went wrong with that request. Please try again.