Skip to content

Commit

Permalink
Fix ID conversion for toJson and toArray
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Apr 15, 2014
1 parent 3f30502 commit 932bb69
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Jenssegers/Mongodb/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,18 @@ public function attributesToArray()
{
$attributes = parent::attributesToArray();

// Because the original Eloquent never returns objects, we convert
// MongoDB related objects to a string representation. This kind
// of mimics the SQL behaviour so that dates are formatted
// nicely when your models are converted to JSON.
foreach ($attributes as &$value)
{
/**
* Here we convert MongoDate instances to string. This mimics
* original SQL behaviour so that dates are formatted nicely
* when your models are converted to JSON.
*/
if ($value instanceof MongoDate)
if ($value instanceof MongoId)
{
$value = (string) $value;
}

else if ($value instanceof MongoDate)
{
$value = $this->asDateTime($value)->format($this->getDateFormat());
}
Expand Down
1 change: 1 addition & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ public function testToArray()
$this->assertEquals(array('_id', 'created_at', 'name', 'type', 'updated_at'), $keys);
$this->assertTrue(is_string($array['created_at']));
$this->assertTrue(is_string($array['updated_at']));
$this->assertTrue(is_string($array['_id']));
}

public function testUnset()
Expand Down

0 comments on commit 932bb69

Please sign in to comment.