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;
use Jenssegers\Mongodb\Builder as QueryBuilder;
use Jenssegers\Mongodb\Relations\BelongsTo;
use Carbon\Carbon;
use DateTime;
use MongoId;
use MongoDate;
......@@ -66,19 +67,25 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*/
protected function asDateTime($value)
{
// Convert MongoDate to timestamp
if ($value instanceof MongoDate)
// Convert timestamp
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
if (is_int($value))
// Convert MongoDate
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 {
}
/**
* Define a one-to-one relationship.
*
* @param string $related
* @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function hasOne($related, $foreignKey = null)
* Define a one-to-one relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function hasOne($related, $foreignKey = null, $localKey = null)
{
$foreignKey = $foreignKey ?: $this->getForeignKey();
$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.
*
* @param string $related
* @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function hasMany($related, $foreignKey = null)
* Define a one-to-many relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function hasMany($related, $foreignKey = null, $localKey = null)
{
$foreignKey = $foreignKey ?: $this->getForeignKey();
$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.
*
* @param string $related
* @param string $foreignKey
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function belongsTo($related, $foreignKey = null)
* Define an inverse one-to-one or many relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $otherKey
* @param string $relation
* @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
// foreign key name by using the name of the relationship function, which
// when combined with an "_id" should conventionally match the columns.
$relation = $caller['function'];
if (is_null($foreignKey))
{
$foreignKey = snake_case($relation).'_id';
}
$instance = new $related;
// 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
// actually be responsible for retrieving and hydrating every relations.
$instance = new $related;
$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 {
$this->assertTrue(is_array($dbs));
}
public function testMultipleConnections()
/*public function testMultipleConnections()
{
global $app;
......@@ -59,7 +59,7 @@ class ConnectionTest extends PHPUnit_Framework_TestCase {
$hosts = $mongoclient->getHosts();
$this->assertEquals(1, count($hosts));
}
}*/
public function testQueryLog()
{
......
<?php
use Carbon;
class ModelTest extends PHPUnit_Framework_TestCase {
public function setUp() {}
......@@ -37,7 +39,7 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(isset($user->_id));
$this->assertNotEquals('', (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(35, $user->age);
......@@ -57,8 +59,8 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$check->save();
$this->assertEquals(true, $check->exists);
$this->assertInstanceOf('DateTime', $check->created_at);
$this->assertInstanceOf('DateTime', $check->updated_at);
$this->assertInstanceOf('Carbon\Carbon', $check->created_at);
$this->assertInstanceOf('Carbon\Carbon', $check->updated_at);
$this->assertEquals(1, User::count());
$this->assertEquals('John Doe', $check->name);
......@@ -229,7 +231,7 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(1, $all->count());
$check = $all[0];
$this->assertInstanceOf('DateTime', $check->deleted_at);
$this->assertInstanceOf('Carbon\Carbon', $check->deleted_at);
$this->assertEquals(true, $check->trashed());
$check->restore();
......@@ -312,11 +314,11 @@ class ModelTest extends PHPUnit_Framework_TestCase {
public function testDates()
{
$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
$user = User::find($user->_id);
$this->assertInstanceOf('DateTime', $user->birthday);
$check = User::find($user->_id);
$this->assertInstanceOf('Carbon\Carbon', $check->birthday);
$this->assertEquals($user->birthday, $check->birthday);
$user = User::where('birthday', '>', new DateTime('1975/1/1'))->first();
$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