Commit 932bb693 authored by Jens Segers's avatar Jens Segers

Fix ID conversion for toJson and toArray

parent 3f30502c
...@@ -195,14 +195,18 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -195,14 +195,18 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
{ {
$attributes = parent::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) foreach ($attributes as &$value)
{ {
/** if ($value instanceof MongoId)
* Here we convert MongoDate instances to string. This mimics {
* original SQL behaviour so that dates are formatted nicely $value = (string) $value;
* when your models are converted to JSON. }
*/
if ($value instanceof MongoDate) else if ($value instanceof MongoDate)
{ {
$value = $this->asDateTime($value)->format($this->getDateFormat()); $value = $this->asDateTime($value)->format($this->getDateFormat());
} }
......
...@@ -287,6 +287,7 @@ class ModelTest extends TestCase { ...@@ -287,6 +287,7 @@ class ModelTest extends TestCase {
$this->assertEquals(array('_id', 'created_at', 'name', 'type', 'updated_at'), $keys); $this->assertEquals(array('_id', 'created_at', 'name', 'type', 'updated_at'), $keys);
$this->assertTrue(is_string($array['created_at'])); $this->assertTrue(is_string($array['created_at']));
$this->assertTrue(is_string($array['updated_at'])); $this->assertTrue(is_string($array['updated_at']));
$this->assertTrue(is_string($array['_id']));
} }
public function testUnset() public function testUnset()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment