Skip to content

Commit 784758e

Browse files
committed
Builder now converts DateTime objects to MongoDate, see issue #51
1 parent 93e53ec commit 784758e

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

Diff for: README.md

+18
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,24 @@ You may also specify additional columns to update:
218218

219219
All basic insert, update, delete and select methods should be implemented.
220220

221+
### Dates
222+
223+
Eloquent allows you to work with Carbon/DateTime objects instead of MongoDate objects. Internally, these dates will be converted to MongoDate objects when saved to the database. If you wish to use this functionality on non-default date fields you will need to manually specify them as described here: http://laravel.com/docs/eloquent#date-mutators
224+
225+
Example:
226+
227+
use Jenssegers\Mongodb\Model as Eloquent;
228+
229+
class User extends Eloquent {
230+
231+
protected $dates = array('birthday');
232+
233+
}
234+
235+
Which allows you to execute queries like:
236+
237+
$users = User::where('birthday', '>', new DateTime('-18 years'))->get();
238+
221239
### Relations
222240

223241
Supported relations are:

Diff for: src/Jenssegers/Mongodb/Builder.php

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
use MongoID;
44
use MongoRegex;
5+
use MongoDate;
6+
use DateTime;
57
use Closure;
68

79
class Builder extends \Illuminate\Database\Query\Builder {
@@ -646,6 +648,12 @@ protected function compileWheres()
646648
}
647649
}
648650

651+
// Convert dates
652+
if (isset($where['value']) && $where['value'] instanceof DateTime)
653+
{
654+
$where['value'] = new MongoDate($where['value']->getTimestamp());
655+
}
656+
649657
// First item of chain
650658
if ($i == 0 && count($this->wheres) > 1 && $where['boolean'] == 'and')
651659
{

Diff for: tests/ModelTest.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,15 @@ public function testUnset()
311311

312312
public function testDates()
313313
{
314-
$user1 = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1')));
315-
$user2 = User::create(array('name' => 'Jane Doe', 'birthday' => new DateTime('1981/1/1')));
316-
317-
$this->assertInstanceOf('DateTime', $user1->birthday);
314+
$user = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1')));
315+
$this->assertInstanceOf('DateTime', $user->birthday);
318316

319317
// Re-fetch to be sure
320-
$user1 = User::find($user1->_id);
321-
$user2 = User::find($user2->_id);
318+
$user = User::find($user->_id);
319+
$this->assertInstanceOf('DateTime', $user->birthday);
322320

323-
$this->assertInstanceOf('DateTime', $user1->birthday);
321+
$user = User::where('birthday', '>', new DateTime('1975/1/1'))->first();
322+
$this->assertEquals('John Doe', $user->name);
324323
}
325324

326325
}

Diff for: tests/QueryBuilderTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,9 @@ public function testDates()
435435
$user = DB::collection('users')->where('birthday', new MongoDate(strtotime("1980-01-01 00:00:00")))->first();
436436
$this->assertEquals('John Doe', $user['name']);
437437

438+
$user = DB::collection('users')->where('birthday', '=', new DateTime("1980-01-01 00:00:00"))->first();
439+
$this->assertEquals('John Doe', $user['name']);
440+
438441
$start = new MongoDate(strtotime("1981-01-01 00:00:00"));
439442
$stop = new MongoDate(strtotime("1982-01-01 00:00:00"));
440443

Diff for: tests/models/User.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
class User extends Eloquent implements UserInterface, RemindableInterface {
99

10-
protected $collection = 'users';
10+
protected $collection = 'users';
1111

1212
protected $dates = array('birthday');
1313

14-
protected static $unguarded = true;
14+
protected static $unguarded = true;
1515

16-
public function books()
16+
public function books()
1717
{
1818
return $this->hasMany('Book', 'author_id');
1919
}

0 commit comments

Comments
 (0)