Skip to content

Commit f38ad32

Browse files
authored
Merge pull request #1357 from Tobion/value-object-id
Cast identifier to string
2 parents ef3a354 + a3e8ef3 commit f38ad32

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

CHANGELOG-4.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ in 4.0 versions.
1212
* Add new options to populate command: --first-page, last-page, --max-per-page. They work only if you use v5 providers API.
1313
* Deprecate some options of populate command: --batch-size and --offset.
1414
* Deprecate Propel support
15+
* Cast value objects used as identifier in Elasticsearch to string
1516

1617
### 4.0.1 (2017-08-10)
1718

Doctrine/Listener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function postFlush()
190190
private function scheduleForDeletion($object)
191191
{
192192
if ($identifierValue = $this->propertyAccessor->getValue($object, $this->config['identifier'])) {
193-
$this->scheduledForDeletion[] = $identifierValue;
193+
$this->scheduledForDeletion[] = !is_scalar($identifierValue) ? (string) $identifierValue : $identifierValue;
194194
}
195195
}
196196

Tests/Transformer/ModelToElasticaAutoTransformerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,20 @@ public function testUnmappedFieldValuesAreNormalisedToStrings()
531531
$this->assertEquals('bar', $data['unmappedValue']);
532532
}
533533

534+
public function testIdentifierIsCastedToString()
535+
{
536+
$idObject = new CastableObject();;
537+
$idObject->foo = '00000000-0000-0000-0000-000000000000';
538+
539+
$object = new \stdClass();
540+
$object->id = $idObject;
541+
542+
$transformer = $this->getTransformer();
543+
$document = $transformer->transform($object, []);
544+
545+
$this->assertSame('00000000-0000-0000-0000-000000000000', $document->getId());
546+
}
547+
534548
/**
535549
* @param null|\Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
536550
*

Transformer/ModelToElasticaAutoTransformer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
6767
**/
6868
public function transform($object, array $fields)
6969
{
70-
$identifier = (string) $this->propertyAccessor->getValue($object, $this->options['identifier']);
70+
$identifier = $this->propertyAccessor->getValue($object, $this->options['identifier']);
71+
if ($identifier && !is_scalar($identifier)) {
72+
$identifier = (string) $identifier;
73+
}
74+
7175
$document = $this->transformObjectToDocument($object, $fields, $identifier);
7276

7377
return $document;

0 commit comments

Comments
 (0)