Commit dd171630 authored by Jens Segers's avatar Jens Segers

MongoDate tweak, fixes #86

parent 6002aca0
...@@ -264,15 +264,30 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -264,15 +264,30 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
{ {
$value = (string) $value; $value = (string) $value;
} }
}
parent::setRawAttributes($attributes, $sync);
}
/**
* Convert the model's attributes to an array.
*
* @return array
*/
public function attributesToArray()
{
$attributes = parent::attributesToArray();
foreach ($attributes as &$value)
{
// Convert MongoDate to string // Convert MongoDate to string
else if ($value instanceof MongoDate) if ($value instanceof MongoDate)
{ {
$value = $this->asDateTime($value)->format('Y-m-d H:i:s'); $value = $this->asDateTime($value)->format('Y-m-d H:i:s');
} }
} }
parent::setRawAttributes($attributes, $sync); return $attributes;
} }
/** /**
......
...@@ -178,6 +178,9 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -178,6 +178,9 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this->assertInstanceOf('Jenssegers\Mongodb\Model', $user); $this->assertInstanceOf('Jenssegers\Mongodb\Model', $user);
$this->assertEquals(true, $user->exists); $this->assertEquals(true, $user->exists);
$this->assertEquals('Jane Poe', $user->name); $this->assertEquals('Jane Poe', $user->name);
$check = User::where('name', 'Jane Poe')->first();
$this->assertEquals($user, $check);
} }
public function testDestroy() public function testDestroy()
...@@ -280,6 +283,14 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -280,6 +283,14 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$items = Item::all(); $items = Item::all();
$this->assertEquals($original, $items->toArray()); $this->assertEquals($original, $items->toArray());
$this->assertEquals($original[0], $items[0]->toArray()); $this->assertEquals($original[0], $items[0]->toArray());
// with date
$item = Item::create(array('name' => 'fork', 'type' => 'sharp'));
$array = $item->toArray();
$this->assertTrue(array_key_exists('created_at', $array));
$this->assertTrue(array_key_exists('updated_at', $array));
$this->assertTrue(is_string($array['created_at']));
$this->assertTrue(is_string($array['updated_at']));
} }
public function testUnset() public function testUnset()
...@@ -312,15 +323,12 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -312,15 +323,12 @@ class ModelTest extends PHPUnit_Framework_TestCase {
public function testDates() public function testDates()
{ {
$user = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1'))); $user = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1')));
$this->assertInstanceOf('Carbon\Carbon', $user->birthday); $this->assertInstanceOf('Carbon\Carbon', $user->birthday);
$check = User::find($user->_id); $check = User::find($user->_id);
$this->assertInstanceOf('Carbon\Carbon', $check->birthday); $this->assertInstanceOf('Carbon\Carbon', $check->birthday);
$this->assertEquals($user->birthday, $check->birthday); $this->assertEquals($user->birthday, $check->birthday);
$user = User::where('birthday', '>', new DateTime('1975/1/1'))->first(); $user = User::where('birthday', '>', new DateTime('1975/1/1'))->first();
$this->assertEquals('John Doe', $user->name); $this->assertEquals('John Doe', $user->name);
} }
......
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