Commit 784758eb authored by Jens Segers's avatar Jens Segers

Builder now converts DateTime objects to MongoDate, see issue #51

parent 93e53ec9
......@@ -218,6 +218,24 @@ You may also specify additional columns to update:
All basic insert, update, delete and select methods should be implemented.
### Dates
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
Example:
use Jenssegers\Mongodb\Model as Eloquent;
class User extends Eloquent {
protected $dates = array('birthday');
}
Which allows you to execute queries like:
$users = User::where('birthday', '>', new DateTime('-18 years'))->get();
### Relations
Supported relations are:
......
......@@ -2,6 +2,8 @@
use MongoID;
use MongoRegex;
use MongoDate;
use DateTime;
use Closure;
class Builder extends \Illuminate\Database\Query\Builder {
......@@ -646,6 +648,12 @@ class Builder extends \Illuminate\Database\Query\Builder {
}
}
// Convert dates
if (isset($where['value']) && $where['value'] instanceof DateTime)
{
$where['value'] = new MongoDate($where['value']->getTimestamp());
}
// First item of chain
if ($i == 0 && count($this->wheres) > 1 && $where['boolean'] == 'and')
{
......
......@@ -311,16 +311,15 @@ class ModelTest extends PHPUnit_Framework_TestCase {
public function testDates()
{
$user1 = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1')));
$user2 = User::create(array('name' => 'Jane Doe', 'birthday' => new DateTime('1981/1/1')));
$this->assertInstanceOf('DateTime', $user1->birthday);
$user = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1')));
$this->assertInstanceOf('DateTime', $user->birthday);
// Re-fetch to be sure
$user1 = User::find($user1->_id);
$user2 = User::find($user2->_id);
$user = User::find($user->_id);
$this->assertInstanceOf('DateTime', $user->birthday);
$this->assertInstanceOf('DateTime', $user1->birthday);
$user = User::where('birthday', '>', new DateTime('1975/1/1'))->first();
$this->assertEquals('John Doe', $user->name);
}
}
......@@ -435,6 +435,9 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$user = DB::collection('users')->where('birthday', new MongoDate(strtotime("1980-01-01 00:00:00")))->first();
$this->assertEquals('John Doe', $user['name']);
$user = DB::collection('users')->where('birthday', '=', new DateTime("1980-01-01 00:00:00"))->first();
$this->assertEquals('John Doe', $user['name']);
$start = new MongoDate(strtotime("1981-01-01 00:00:00"));
$stop = new MongoDate(strtotime("1982-01-01 00:00:00"));
......
......@@ -7,13 +7,13 @@ use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $collection = 'users';
protected $collection = 'users';
protected $dates = array('birthday');
protected static $unguarded = true;
protected static $unguarded = true;
public function books()
public function books()
{
return $this->hasMany('Book', 'author_id');
}
......
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