Commit 0eed8964 authored by unknown's avatar unknown

Models fixed and tests updated

parent bbb79909
......@@ -9,6 +9,7 @@ use Jenssegers\Mongodb\Builder as QueryBuilder;
use Jenssegers\Mongodb\Relations\BelongsTo;
use Jenssegers\Mongodb\Relations\BelongsToMany;
use Carbon\Carbon;
use DateTime;
use MongoId;
use MongoDate;
......@@ -67,19 +68,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))
{
$value = $value->sec;
return Carbon::createFromTimestamp($value);
}
// Convert timestamp to string for DateTime
if (is_int($value))
// Convert string
if (is_string($value))
{
return new Carbon($value);
}
// Convert MongoDate
if ($value instanceof MongoDate)
{
$value = "@$value";
return Carbon::createFromTimestamp($value->sec);
}
return new DateTime($value);
return Carbon::instance($value);
}
/**
......@@ -119,15 +126,18 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*
* @param string $related
* @param string $foreignKey
* @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();
$instance = new $related;
return new HasOne($instance->newQuery(), $this, $foreignKey);
$localKey = $localKey ?: $this->getKeyName();
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
}
/**
......@@ -135,15 +145,18 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*
* @param string $related
* @param string $foreignKey
* @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();
$instance = new $related;
return new HasMany($instance->newQuery(), $this, $foreignKey);
$localKey = $localKey ?: $this->getKeyName();
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
}
/**
......@@ -151,30 +164,40 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*
* @param string $related
* @param string $foreignKey
* @param string $otherKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function belongsTo($related, $foreignKey = null)
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
{
// 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);
}
/**
......@@ -280,6 +303,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*/
public function __call($method, $parameters)
{
// Unset method
if ($method == 'unset')
{
return call_user_func_array(array($this, 'dropColumn'), $parameters);
......
......@@ -312,12 +312,15 @@ class ModelTest extends PHPUnit_Framework_TestCase {
public function testDates()
{
$user = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1')));
$this->assertInstanceOf('Carbon\Carbon', $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);
}
......
......@@ -3,11 +3,6 @@
class RelationsTest extends PHPUnit_Framework_TestCase {
public function setUp() {
User::truncate();
Book::truncate();
Item::truncate();
Role::truncate();
Client::truncate();
}
public function tearDown()
......@@ -108,6 +103,29 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('admin', $role->type);
}
public function testEasyRelation()
{
// Has Many
$user = User::create(array('name' => 'John Doe'));
$item = Item::create(array('type' => 'knife'));
$user->items()->save($item);
$user = User::find($user->_id);
$items = $user->items;
$this->assertEquals(1, count($items));
$this->assertInstanceOf('Item', $items[0]);
// Has one
$user = User::create(array('name' => 'John Doe'));
$role = Role::create(array('type' => 'admin'));
$user->role()->save($role);
$user = User::find($user->_id);
$role = $user->role;
$this->assertInstanceOf('Role', $role);
$this->assertEquals('admin', $role->type);
}
public function testHasManyAndBelongsTo()
{
$user = User::create(array('name' => 'John Doe'));
......@@ -176,6 +194,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Client::create(array('name' => 'Meatballs Inc.'))->_id
);
// Sync multiple records
$user->clients()->sync($clients);
$user = User::with('clients')->find($user->_id);
......
......@@ -9,6 +9,8 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
protected $collection = 'users';
protected $dates = array('birthday');
protected static $unguarded = true;
public function books()
......@@ -60,5 +62,4 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
{
return $this->email;
}
}
\ No newline at end of file
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