Skip to content

Commit 51b3234

Browse files
committed
MongoDate tweak, fixes #86
1 parent 6727b70 commit 51b3234

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,30 @@ public function setRawAttributes(array $attributes, $sync = false)
273273
{
274274
$value = (string) $value;
275275
}
276+
}
277+
278+
parent::setRawAttributes($attributes, $sync);
279+
}
276280

281+
/**
282+
* Convert the model's attributes to an array.
283+
*
284+
* @return array
285+
*/
286+
public function attributesToArray()
287+
{
288+
$attributes = parent::attributesToArray();
289+
290+
foreach ($attributes as &$value)
291+
{
277292
// Convert MongoDate to string
278-
else if ($value instanceof MongoDate)
293+
if ($value instanceof MongoDate)
279294
{
280295
$value = $this->asDateTime($value)->format('Y-m-d H:i:s');
281296
}
282297
}
283298

284-
parent::setRawAttributes($attributes, $sync);
299+
return $attributes;
285300
}
286301

287302
/**

tests/ModelTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ public function testCreate()
178178
$this->assertInstanceOf('Jenssegers\Mongodb\Model', $user);
179179
$this->assertEquals(true, $user->exists);
180180
$this->assertEquals('Jane Poe', $user->name);
181+
182+
$check = User::where('name', 'Jane Poe')->first();
183+
$this->assertEquals($user, $check);
181184
}
182185

183186
public function testDestroy()
@@ -280,6 +283,14 @@ public function testToArray()
280283
$items = Item::all();
281284
$this->assertEquals($original, $items->toArray());
282285
$this->assertEquals($original[0], $items[0]->toArray());
286+
287+
// with date
288+
$item = Item::create(array('name' => 'fork', 'type' => 'sharp'));
289+
$array = $item->toArray();
290+
$this->assertTrue(array_key_exists('created_at', $array));
291+
$this->assertTrue(array_key_exists('updated_at', $array));
292+
$this->assertTrue(is_string($array['created_at']));
293+
$this->assertTrue(is_string($array['updated_at']));
283294
}
284295

285296
public function testUnset()
@@ -312,14 +323,11 @@ public function testUnset()
312323
public function testDates()
313324
{
314325
$user = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1')));
315-
316326
$this->assertInstanceOf('Carbon\Carbon', $user->birthday);
317327

318328
$check = User::find($user->_id);
319-
320329
$this->assertInstanceOf('Carbon\Carbon', $check->birthday);
321330
$this->assertEquals($user->birthday, $check->birthday);
322-
323331

324332
$user = User::where('birthday', '>', new DateTime('1975/1/1'))->first();
325333
$this->assertEquals('John Doe', $user->name);

tests/QueryBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ public function testDistinct()
265265
));
266266

267267
$items = DB::collection('items')->distinct('name')->get();
268-
$this->assertEquals(array('knife', 'fork', 'spoon'), $items);
268+
$this->assertEquals(3, count($items));
269269

270270
$types = DB::collection('items')->distinct('type')->get();
271-
$this->assertEquals(array('sharp', 'round'), $types);
271+
$this->assertEquals(2, count($types));
272272
}
273273

274274
public function testCustomId()

0 commit comments

Comments
 (0)