Skip to content
Snippets Groups Projects
Commit 4086829b authored by catch's avatar catch
Browse files

Issue #1003788 by stefan.r, Alan D., Josh Waihi, JimmyAx, robhardwick, wiifm,...

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.
parent 1dbabcaa
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -342,6 +342,10 @@ public function getTableMapping() {
* {@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 @@ protected function doLoadMultiple(array $ids = NULL) {
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.
*
......
......@@ -76,6 +76,11 @@ function testEntityViewController() {
$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);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment