Issue #1003788 by stefan.r, Alan D., Josh Waihi, JimmyAx, robhardwick, wiifm, twistor, pwolanin, bzrudi71, bellHead, john_brown: Fixed PostgreSQL: PDOException:Invalid text representation when attempting to load an entity with a string or non-scalar ID.

8.0.x
Nathaniel Catchpole 2014-06-20 09:07:57 +01:00
parent 1dbabcaaa6
commit 4086829bbe
2 changed files with 42 additions and 0 deletions

View File

@ -342,6 +342,10 @@ class ContentEntityDatabaseStorage extends ContentEntityStorageBase implements S
* {@inheritdoc}
*/
protected function doLoadMultiple(array $ids = NULL) {
if (!empty($ids)) {
$ids = $this->cleanIds($ids);
}
// Build and execute the query.
$records = $this
->buildQuery($ids)
@ -351,6 +355,39 @@ class ContentEntityDatabaseStorage extends ContentEntityStorageBase implements S
return $this->mapFromStorageRecords($records);
}
/**
* Sanitizes the entity IDs to the correct data type.
*
* The identifier sanitization provided by this method has been introduced
* as Drupal used to rely on the database to facilitate this, which worked
* correctly with MySQL but led to errors with other DBMS such as PostgeSQL.
*
* @param array $ids
* The entity IDs to verify.
* @return array
* The sanitized list of entity IDs.
*/
protected function cleanIds(array $ids) {
$keys = $this->entityType->getKeys();
$bundle = $keys['bundle'] ? $keys['bundle'] : $this->entityTypeId;
$definitions = $this->entityManager->getFieldDefinitions($this->entityTypeId, $bundle);
$info = $this->entityManager->getDefinition($this->entityTypeId);
$id_definition = $definitions[$info->getKey('id')];
switch ($id_definition->getType()) {
case 'integer':
$ids = array_filter($ids, 'is_numeric');
$ids = array_map('intval', $ids);
break;
case 'string':
$ids = array_filter($ids, 'is_string');
$ids = array_map('strval', $ids);
break;
}
return $ids;
}
/**
* Maps from storage records to entity objects.
*

View File

@ -76,6 +76,11 @@ class EntityViewControllerTest extends WebTestBase {
$this->assertRaw($entity->label());
$this->assertRaw('full');
}
// As entity_test IDs must be integers, make sure requests for non-integer
// IDs return a page not found error.
$this->drupalGet('entity_test/invalid');
$this->assertResponse(404);
}
/**