Commit 9cf8823f authored by Jens Segers's avatar Jens Segers

Switching to Carbon like Laravel

parent 784758eb
...@@ -8,6 +8,7 @@ use Jenssegers\Mongodb\DatabaseManager as Resolver; ...@@ -8,6 +8,7 @@ use Jenssegers\Mongodb\DatabaseManager as Resolver;
use Jenssegers\Mongodb\Builder as QueryBuilder; use Jenssegers\Mongodb\Builder as QueryBuilder;
use Jenssegers\Mongodb\Relations\BelongsTo; use Jenssegers\Mongodb\Relations\BelongsTo;
use Carbon\Carbon;
use DateTime; use DateTime;
use MongoId; use MongoId;
use MongoDate; use MongoDate;
...@@ -66,19 +67,25 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -66,19 +67,25 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*/ */
protected function asDateTime($value) protected function asDateTime($value)
{ {
// Convert MongoDate to timestamp // Convert timestamp
if ($value instanceof MongoDate) if (is_numeric($value))
{
return Carbon::createFromTimestamp($value);
}
// Convert string
if (is_string($value))
{ {
$value = $value->sec; return new Carbon($value);
} }
// Convert timestamp to string for DateTime // Convert MongoDate
if (is_int($value)) if ($value instanceof MongoDate)
{ {
$value = "@$value"; return Carbon::createFromTimestamp($value->sec);
} }
return new DateTime($value); return Carbon::instance($value);
} }
/** /**
...@@ -114,66 +121,82 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -114,66 +121,82 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
} }
/** /**
* Define a one-to-one relationship. * Define a one-to-one relationship.
* *
* @param string $related * @param string $related
* @param string $foreignKey * @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\HasOne * @param string $localKey
*/ * @return \Illuminate\Database\Eloquent\Relations\HasOne
public function hasOne($related, $foreignKey = null) */
public function hasOne($related, $foreignKey = null, $localKey = null)
{ {
$foreignKey = $foreignKey ?: $this->getForeignKey(); $foreignKey = $foreignKey ?: $this->getForeignKey();
$instance = new $related; $instance = new $related;
return new HasOne($instance->newQuery(), $this, $foreignKey); $localKey = $localKey ?: $this->getKeyName();
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
} }
/** /**
* Define a one-to-many relationship. * Define a one-to-many relationship.
* *
* @param string $related * @param string $related
* @param string $foreignKey * @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @param string $localKey
*/ * @return \Illuminate\Database\Eloquent\Relations\HasMany
public function hasMany($related, $foreignKey = null) */
public function hasMany($related, $foreignKey = null, $localKey = null)
{ {
$foreignKey = $foreignKey ?: $this->getForeignKey(); $foreignKey = $foreignKey ?: $this->getForeignKey();
$instance = new $related; $instance = new $related;
return new HasMany($instance->newQuery(), $this, $foreignKey); $localKey = $localKey ?: $this->getKeyName();
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
} }
/** /**
* Define an inverse one-to-one or many relationship. * Define an inverse one-to-one or many relationship.
* *
* @param string $related * @param string $related
* @param string $foreignKey * @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo * @param string $otherKey
*/ * @param string $relation
public function belongsTo($related, $foreignKey = null) * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
{ {
list(, $caller) = debug_backtrace(false); // If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relatinoships.
if (is_null($relation))
{
list(, $caller) = debug_backtrace(false);
$relation = $caller['function'];
}
// If no foreign key was supplied, we can use a backtrace to guess the proper // If no foreign key was supplied, we can use a backtrace to guess the proper
// foreign key name by using the name of the relationship function, which // foreign key name by using the name of the relationship function, which
// when combined with an "_id" should conventionally match the columns. // when combined with an "_id" should conventionally match the columns.
$relation = $caller['function'];
if (is_null($foreignKey)) if (is_null($foreignKey))
{ {
$foreignKey = snake_case($relation).'_id'; $foreignKey = snake_case($relation).'_id';
} }
$instance = new $related;
// Once we have the foreign key names, we'll just create a new Eloquent query // Once we have the foreign key names, we'll just create a new Eloquent query
// for the related models and returns the relationship instance which will // for the related models and returns the relationship instance which will
// actually be responsible for retrieving and hydrating every relations. // actually be responsible for retrieving and hydrating every relations.
$instance = new $related;
$query = $instance->newQuery(); $query = $instance->newQuery();
return new BelongsTo($query, $this, $foreignKey, $relation); $otherKey = $otherKey ?: $instance->getKeyName();
return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
} }
/** /**
......
...@@ -46,7 +46,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase { ...@@ -46,7 +46,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(is_array($dbs)); $this->assertTrue(is_array($dbs));
} }
public function testMultipleConnections() /*public function testMultipleConnections()
{ {
global $app; global $app;
...@@ -59,7 +59,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase { ...@@ -59,7 +59,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
$hosts = $mongoclient->getHosts(); $hosts = $mongoclient->getHosts();
$this->assertEquals(1, count($hosts)); $this->assertEquals(1, count($hosts));
} }*/
public function testQueryLog() public function testQueryLog()
{ {
......
<?php <?php
use Carbon;
class ModelTest extends PHPUnit_Framework_TestCase { class ModelTest extends PHPUnit_Framework_TestCase {
public function setUp() {} public function setUp() {}
...@@ -37,7 +39,7 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -37,7 +39,7 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(isset($user->_id)); $this->assertTrue(isset($user->_id));
$this->assertNotEquals('', (string) $user->_id); $this->assertNotEquals('', (string) $user->_id);
$this->assertNotEquals(0, strlen((string) $user->_id)); $this->assertNotEquals(0, strlen((string) $user->_id));
$this->assertInstanceOf('DateTime', $user->created_at); $this->assertInstanceOf('Carbon\Carbon', $user->created_at);
$this->assertEquals('John Doe', $user->name); $this->assertEquals('John Doe', $user->name);
$this->assertEquals(35, $user->age); $this->assertEquals(35, $user->age);
...@@ -57,8 +59,8 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -57,8 +59,8 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$check->save(); $check->save();
$this->assertEquals(true, $check->exists); $this->assertEquals(true, $check->exists);
$this->assertInstanceOf('DateTime', $check->created_at); $this->assertInstanceOf('Carbon\Carbon', $check->created_at);
$this->assertInstanceOf('DateTime', $check->updated_at); $this->assertInstanceOf('Carbon\Carbon', $check->updated_at);
$this->assertEquals(1, User::count()); $this->assertEquals(1, User::count());
$this->assertEquals('John Doe', $check->name); $this->assertEquals('John Doe', $check->name);
...@@ -229,7 +231,7 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -229,7 +231,7 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(1, $all->count()); $this->assertEquals(1, $all->count());
$check = $all[0]; $check = $all[0];
$this->assertInstanceOf('DateTime', $check->deleted_at); $this->assertInstanceOf('Carbon\Carbon', $check->deleted_at);
$this->assertEquals(true, $check->trashed()); $this->assertEquals(true, $check->trashed());
$check->restore(); $check->restore();
...@@ -312,11 +314,11 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -312,11 +314,11 @@ 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('DateTime', $user->birthday); $this->assertInstanceOf('Carbon\Carbon', $user->birthday);
// Re-fetch to be sure $check = User::find($user->_id);
$user = User::find($user->_id); $this->assertInstanceOf('Carbon\Carbon', $check->birthday);
$this->assertInstanceOf('DateTime', $user->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