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: ...@@ -218,6 +218,24 @@ You may also specify additional columns to update:
All basic insert, update, delete and select methods should be implemented. 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 ### Relations
Supported relations are: Supported relations are:
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
use MongoID; use MongoID;
use MongoRegex; use MongoRegex;
use MongoDate;
use DateTime;
use Closure; use Closure;
class Builder extends \Illuminate\Database\Query\Builder { class Builder extends \Illuminate\Database\Query\Builder {
...@@ -646,6 +648,12 @@ 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 // First item of chain
if ($i == 0 && count($this->wheres) > 1 && $where['boolean'] == 'and') if ($i == 0 && count($this->wheres) > 1 && $where['boolean'] == 'and')
{ {
......
...@@ -311,16 +311,15 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -311,16 +311,15 @@ class ModelTest extends PHPUnit_Framework_TestCase {
public function testDates() public function testDates()
{ {
$user1 = 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')));
$user2 = User::create(array('name' => 'Jane Doe', 'birthday' => new DateTime('1981/1/1'))); $this->assertInstanceOf('DateTime', $user->birthday);
$this->assertInstanceOf('DateTime', $user1->birthday);
// Re-fetch to be sure // Re-fetch to be sure
$user1 = User::find($user1->_id); $user = User::find($user->_id);
$user2 = User::find($user2->_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 { ...@@ -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(); $user = DB::collection('users')->where('birthday', new MongoDate(strtotime("1980-01-01 00:00:00")))->first();
$this->assertEquals('John Doe', $user['name']); $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")); $start = new MongoDate(strtotime("1981-01-01 00:00:00"));
$stop = new MongoDate(strtotime("1982-01-01 00:00:00")); $stop = new MongoDate(strtotime("1982-01-01 00:00:00"));
......
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