Skip to content

Commit

Permalink
Merge pull request #1357 from Tobion/value-object-id
Browse files Browse the repository at this point in the history
Cast identifier to string
  • Loading branch information
Tobion authored Dec 17, 2017
2 parents ef3a354 + a3e8ef3 commit f38ad32
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion Doctrine/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
14 changes: 14 additions & 0 deletions Tests/Transformer/ModelToElasticaAutoTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
6 changes: 5 additions & 1 deletion Transformer/ModelToElasticaAutoTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f38ad32

Please sign in to comment.