From 3026d583fb672fe47f89fd9d06d027c1cab4f98e Mon Sep 17 00:00:00 2001 From: Martynas Sudintas Date: Thu, 6 Nov 2014 15:18:13 +0200 Subject: [PATCH 1/2] Added support for \DateTime in date property --- ORM/Manager.php | 3 ++ Result/Converter.php | 11 ++++++- Tests/Functional/ORM/ManagerTest.php | 22 ++++++------- Tests/Unit/Mapping/MetadataCollectorTest.php | 3 ++ Tests/Unit/Result/DocumentIteratorTest.php | 5 +++ .../Acme/TestBundle/Document/Comment.php | 31 +++++++++++++++++++ 6 files changed, 63 insertions(+), 12 deletions(-) diff --git a/ORM/Manager.php b/ORM/Manager.php index 7da28f16..a8cdba3f 100644 --- a/ORM/Manager.php +++ b/ORM/Manager.php @@ -156,6 +156,9 @@ private function convertToArray($object, $getters) $value = $newValue; } + if ($value instanceof \DateTimeInterface) { + $value = $value->format(\DateTime::ISO8601); + } if ($value) { $document[$field] = $value; diff --git a/Result/Converter.php b/Result/Converter.php index dd3731a6..78c80385 100644 --- a/Result/Converter.php +++ b/Result/Converter.php @@ -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'])); @@ -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 { diff --git a/Tests/Functional/ORM/ManagerTest.php b/Tests/Functional/ORM/ManagerTest.php index b4d17956..7048a7f5 100644 --- a/Tests/Functional/ORM/ManagerTest.php +++ b/Tests/Functional/ORM/ManagerTest.php @@ -80,14 +80,14 @@ 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'; - - $manager->persist($product); + $comment = new Comment(); + $comment->setId('testId'); + $comment->setTtl(500000); + $comment->setScore('1.0'); + $comment->setParent('parentId'); + $comment->userName = 'testUser'; + + $manager->persist($comment); $manager->commit(); $repository = $manager->getRepository('AcmeTestBundle:Comment'); @@ -96,8 +96,8 @@ 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->assertEquals($comment->getId(), $actualProduct->getId()); + $this->assertEquals($comment->getParent(), $actualProduct->getParent()); + $this->assertLessThan($comment->getTtl(), $actualProduct->getTtl()); } } diff --git a/Tests/Unit/Mapping/MetadataCollectorTest.php b/Tests/Unit/Mapping/MetadataCollectorTest.php index c24b62f7..1c85c7b1 100644 --- a/Tests/Unit/Mapping/MetadataCollectorTest.php +++ b/Tests/Unit/Mapping/MetadataCollectorTest.php @@ -168,6 +168,9 @@ public function testGet() 'userName' => [ 'type' => 'string', ], + 'createdAt' => [ + 'type' => 'date' + ] ], ], ], diff --git a/Tests/Unit/Result/DocumentIteratorTest.php b/Tests/Unit/Result/DocumentIteratorTest.php index 01e2a510..d0c39323 100644 --- a/Tests/Unit/Result/DocumentIteratorTest.php +++ b/Tests/Unit/Result/DocumentIteratorTest.php @@ -87,6 +87,11 @@ public function testIteration($rawData) 'name' => 'header', ] ], + 'properties' => [ + 'header' => [ + 'type' => 'string' + ] + ], 'namespace' => 'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Content', ], ]; diff --git a/Tests/app/fixture/Acme/TestBundle/Document/Comment.php b/Tests/app/fixture/Acme/TestBundle/Document/Comment.php index 6a8f07f6..eb13c59e 100644 --- a/Tests/app/fixture/Acme/TestBundle/Document/Comment.php +++ b/Tests/app/fixture/Acme/TestBundle/Document/Comment.php @@ -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; + } } From 27652e0632f5bf63ec9588834941c831795e47c8 Mon Sep 17 00:00:00 2001 From: Martynas Sudintas Date: Thu, 6 Nov 2014 15:50:02 +0200 Subject: [PATCH 2/2] persisting \DateTime test --- ORM/Manager.php | 2 +- Tests/Functional/ORM/ManagerTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/ORM/Manager.php b/ORM/Manager.php index a8cdba3f..606e964a 100644 --- a/ORM/Manager.php +++ b/ORM/Manager.php @@ -156,7 +156,7 @@ private function convertToArray($object, $getters) $value = $newValue; } - if ($value instanceof \DateTimeInterface) { + if ($value instanceof \DateTime) { $value = $value->format(\DateTime::ISO8601); } diff --git a/Tests/Functional/ORM/ManagerTest.php b/Tests/Functional/ORM/ManagerTest.php index 7048a7f5..9266a9cb 100644 --- a/Tests/Functional/ORM/ManagerTest.php +++ b/Tests/Functional/ORM/ManagerTest.php @@ -100,4 +100,29 @@ public function testPersistSpecialFields() $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'); + $search = $repository->createSearch(); + $results = $repository->execute($search); + /** @var DocumentInterface $actualProduct */ + $actualProduct = $results[0]; + + $this->assertGreaterThan(time(), $actualProduct->getCreatedAt()->getTimestamp()); + } }