Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Hydrator not acception DateTime objects on @ODM\Date fields #787

Merged
merged 1 commit into from

2 participants

@stephandesouza
Exception: DateTime::__construct() expects parameter 1 to be string, object given
tests\Hydrators\DoctrineODMMongoDBTestsHydrationClosureUserHydrator.php:49
lib\Doctrine\ODM\MongoDB\Hydrator\HydratorFactory.php:428
tests\Doctrine\ODM\MongoDB\Tests\HydratorTest.php:31
@stephandesouza stephandesouza Hydrator not acception DateTime objects on @ODM\Date fields
~~

Exception: DateTime::__construct() expects parameter 1 to be string, object given
tests\Hydrators\DoctrineODMMongoDBTestsHydrationClosureUserHydrator.php:49
lib\Doctrine\ODM\MongoDB\Hydrator\HydratorFactory.php:428
tests\Doctrine\ODM\MongoDB\Tests\HydratorTest.php:31
8e3b5fc
@jmikola
Owner

Thanks for the fix. The convertToPHPValue() and closureToPHP() methods should always be in sync, ignoring the null checking in the latter.

Hypothetically, how are you ever getting a DateTime object in your raw MongoDB document?

@jmikola jmikola merged commit dec2bda into doctrine:master
@stephandesouza

The DateTime object is stored as MongoDate, is not supposed to be?

MongoDate::__set_state(array(
     'sec' => 1390937488,
     'usec' => 0,
  )),
@jmikola
Owner

Yes, but hydration entails converting the raw result from the MongoDB driver (after unserializing BSON) into a PHP value. BSON unserialization would never produce a DateTime object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 28, 2014
  1. @stephandesouza

    Hydrator not acception DateTime objects on @ODM\Date fields

    stephandesouza authored
    ~~
    
    Exception: DateTime::__construct() expects parameter 1 to be string, object given
    tests\Hydrators\DoctrineODMMongoDBTestsHydrationClosureUserHydrator.php:49
    lib\Doctrine\ODM\MongoDB\Hydrator\HydratorFactory.php:428
    tests\Doctrine\ODM\MongoDB\Tests\HydratorTest.php:31
This page is out of date. Refresh to see the latest.
View
2  lib/Doctrine/ODM/MongoDB/Types/DateType.php
@@ -76,6 +76,6 @@ public function closureToMongo()
public function closureToPHP()
{
- return 'if ($value instanceof \MongoDate) { $return = new \DateTime(); $return->setTimestamp($value->sec); } elseif (is_numeric($value)) { $return = new \DateTime(); $return->setTimestamp($value); } else { $return = new \DateTime($value); }';
+ return 'if ($value instanceof \MongoDate) { $return = new \DateTime(); $return->setTimestamp($value->sec); } elseif (is_numeric($value)) { $return = new \DateTime(); $return->setTimestamp($value); } elseif ($value instanceof \DateTime) { $return = $value; } else { $return = new \DateTime($value); }';
}
}
View
5 tests/Doctrine/ODM/MongoDB/Tests/HydratorTest.php
@@ -14,6 +14,7 @@ public function testHydrator()
$this->dm->getHydratorFactory()->hydrate($user, array(
'_id' => 1,
'name' => 'jon',
+ 'birthdate' => new \DateTime('1961-01-01'),
'referenceOne' => array('$id' => '1'),
'referenceMany' => array(
array(
@@ -31,6 +32,7 @@ public function testHydrator()
$this->assertEquals(1, $user->id);
$this->assertEquals('jon', $user->name);
+ $this->assertInstanceOf('DateTime', $user->birthdate);
$this->assertInstanceOf(__NAMESPACE__.'\HydrationClosureReferenceOne', $user->referenceOne);
$this->assertInstanceOf('Doctrine\ODM\MongoDB\PersistentCollection', $user->referenceMany);
$this->assertInstanceOf('Doctrine\ODM\MongoDB\Proxy\Proxy', $user->referenceMany[0]);
@@ -49,6 +51,9 @@ class HydrationClosureUser
/** @ODM\String */
public $name;
+
+ /** @ODM\Date */
+ public $birthdate;
/** @ODM\ReferenceOne(targetDocument="HydrationClosureReferenceOne") */
public $referenceOne;
Something went wrong with that request. Please try again.