diff --git a/CHANGELOG-4.0.md b/CHANGELOG-4.0.md index cc0359018..e3d8e404b 100644 --- a/CHANGELOG-4.0.md +++ b/CHANGELOG-4.0.md @@ -12,6 +12,7 @@ in 4.0 versions. * Add new options to populate command: --first-page, last-page, --max-per-page. They work only if you use v5 providers API. * Deprecate some options of populate command: --batch-size and --offset. * Deprecate Propel support +* Cast value objects used as identifier in Elasticsearch to string ### 4.0.1 (2017-08-10) diff --git a/Doctrine/Listener.php b/Doctrine/Listener.php index a1d35850a..1fe1efa29 100644 --- a/Doctrine/Listener.php +++ b/Doctrine/Listener.php @@ -190,7 +190,7 @@ public function postFlush() private function scheduleForDeletion($object) { if ($identifierValue = $this->propertyAccessor->getValue($object, $this->config['identifier'])) { - $this->scheduledForDeletion[] = $identifierValue; + $this->scheduledForDeletion[] = !is_scalar($identifierValue) ? (string) $identifierValue : $identifierValue; } } diff --git a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php index f43faedac..e9ac80a6f 100644 --- a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php +++ b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php @@ -531,6 +531,20 @@ public function testUnmappedFieldValuesAreNormalisedToStrings() $this->assertEquals('bar', $data['unmappedValue']); } + public function testIdentifierIsCastedToString() + { + $idObject = new CastableObject();; + $idObject->foo = '00000000-0000-0000-0000-000000000000'; + + $object = new \stdClass(); + $object->id = $idObject; + + $transformer = $this->getTransformer(); + $document = $transformer->transform($object, []); + + $this->assertSame('00000000-0000-0000-0000-000000000000', $document->getId()); + } + /** * @param null|\Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher * diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index 413cca4f7..1229be13b 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -67,7 +67,11 @@ public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor) **/ public function transform($object, array $fields) { - $identifier = (string) $this->propertyAccessor->getValue($object, $this->options['identifier']); + $identifier = $this->propertyAccessor->getValue($object, $this->options['identifier']); + if ($identifier && !is_scalar($identifier)) { + $identifier = (string) $identifier; + } + $document = $this->transformObjectToDocument($object, $fields, $identifier); return $document;