Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ORM/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ private function convertToArray($object, $getters)

$value = $newValue;
}
if ($value instanceof \DateTime) {
$value = $value->format(\DateTime::ISO8601);
}

if ($value) {
$document[$field] = $value;
Expand Down
11 changes: 10 additions & 1 deletion Result/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ public function convertToDocument($rawData)
$data = isset($rawData['_source']) ? $rawData['_source'] : array_map('reset', $rawData['fields']);

/** @var DocumentInterface $object */
$object = $this->assignArrayToObject($data, new $metadata['namespace'](), $metadata['setters']);
$object = $this->assignArrayToObject(
$data,
new $metadata['namespace'](),
array_merge_recursive($metadata['properties'], $metadata['setters'])
);

isset($rawData['_id']) && $object->setId($rawData['_id']);
isset($rawData['_score']) && $object->setScore($rawData['_score']);
isset($rawData['highlight']) && $object->setHighlight(new DocumentHighlight($rawData['highlight']));
Expand Down Expand Up @@ -90,6 +95,10 @@ public function assignArrayToObject(array $array, $object, array $setters)
$value = new ObjectIterator($this, $value, $setter);
}

if ($setter['type'] === 'date') {
$value = \DateTime::createFromFormat(\DateTime::ISO8601, $value);
}

if ($setter['exec']) {
$object->{$setter['name']}($value);
} else {
Expand Down
45 changes: 35 additions & 10 deletions Tests/Functional/ORM/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,41 @@ public function testPersistSpecialFields()
/** @var Manager $manager */
$manager = $this->getManager();

$product = new Comment();
$product->setId('testId');
$product->setTtl(500000);
$product->setScore('1.0');
$product->setParent('parentId');
$product->userName = 'testUser';
$comment = new Comment();
$comment->setId('testId');
$comment->setTtl(500000);
$comment->setScore('1.0');
$comment->setParent('parentId');
$comment->userName = 'testUser';

$manager->persist($comment);
$manager->commit();

$manager->persist($product);
$repository = $manager->getRepository('AcmeTestBundle:Comment');
$search = $repository->createSearch();
$results = $repository->execute($search);
/** @var DocumentInterface $actualProduct */
$actualProduct = $results[0];

$this->assertEquals($comment->getId(), $actualProduct->getId());
$this->assertEquals($comment->getParent(), $actualProduct->getParent());
$this->assertLessThan($comment->getTtl(), $actualProduct->getTtl());
}

/**
* Tests if DateTime object is being parsed.
*/
public function testPersistDateField()
{
/** @var Manager $manager */
$manager = $this->getManager();

$comment = new Comment();
$comment->setId('testId');
$comment->setParent('parentId');
$comment->setCreatedAt(new \DateTime('2100-01-02 03:04:05.889342'));

$manager->persist($comment);
$manager->commit();

$repository = $manager->getRepository('AcmeTestBundle:Comment');
Expand All @@ -96,8 +123,6 @@ public function testPersistSpecialFields()
/** @var DocumentInterface $actualProduct */
$actualProduct = $results[0];

$this->assertEquals($product->getId(), $actualProduct->getId());
$this->assertEquals($product->getParent(), $actualProduct->getParent());
$this->assertLessThan($product->getTtl(), $actualProduct->getTtl());
$this->assertGreaterThan(time(), $actualProduct->getCreatedAt()->getTimestamp());
}
}
3 changes: 3 additions & 0 deletions Tests/Unit/Mapping/MetadataCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ public function testGet()
'userName' => [
'type' => 'string',
],
'createdAt' => [
'type' => 'date'
]
],
],
],
Expand Down
5 changes: 5 additions & 0 deletions Tests/Unit/Result/DocumentIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public function testIteration($rawData)
'name' => 'header',
]
],
'properties' => [
'header' => [
'type' => 'string'
]
],
'namespace' => 'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Content',
],
];
Expand Down
31 changes: 31 additions & 0 deletions Tests/app/fixture/Acme/TestBundle/Document/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,35 @@ class Comment implements DocumentInterface
* @ES\Property(type="string", name="userName")
*/
public $userName;

/**
* @var \DateTime
*
* @ES\Property(name="createdAt", type="date")
*/
private $createdAt;

/**
* Constructor.
*/
public function __construct()
{
$this->createdAt = new \DateTime();
}

/**
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}

/**
* @param \DateTime $createdAt
*/
public function setCreatedAt(\DateTime $createdAt)
{
$this->createdAt = $createdAt;
}
}