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; ...@@ -9,6 +9,7 @@ use Jenssegers\Mongodb\Builder as QueryBuilder;
use Jenssegers\Mongodb\Relations\BelongsTo; use Jenssegers\Mongodb\Relations\BelongsTo;
use Jenssegers\Mongodb\Relations\BelongsToMany; use Jenssegers\Mongodb\Relations\BelongsToMany;
use Carbon\Carbon;
use DateTime; use DateTime;
use MongoId; use MongoId;
use MongoDate; use MongoDate;
...@@ -67,19 +68,25 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -67,19 +68,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);
} }
/** /**
...@@ -115,68 +122,84 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -115,68 +122,84 @@ 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);
} }
/** /**
* Define a many-to-many relationship. * Define a many-to-many relationship.
* *
...@@ -266,7 +289,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -266,7 +289,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
{ {
$this->__unset($column); $this->__unset($column);
} }
// Perform unset only on current document // Perform unset only on current document
return $query = $this->newQuery()->where($this->getKeyName(), $this->getKey())->unset($columns); return $query = $this->newQuery()->where($this->getKeyName(), $this->getKey())->unset($columns);
} }
...@@ -280,6 +303,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -280,6 +303,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
*/ */
public function __call($method, $parameters) public function __call($method, $parameters)
{ {
// Unset method
if ($method == 'unset') if ($method == 'unset')
{ {
return call_user_func_array(array($this, 'dropColumn'), $parameters); return call_user_func_array(array($this, 'dropColumn'), $parameters);
......
...@@ -312,11 +312,14 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -312,11 +312,14 @@ 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('Carbon\Carbon', $user->birthday); $this->assertInstanceOf('Carbon\Carbon', $user->birthday);
$check = User::find($user->_id); $check = User::find($user->_id);
$this->assertInstanceOf('Carbon\Carbon', $check->birthday); $this->assertInstanceOf('Carbon\Carbon', $check->birthday);
$this->assertEquals($user->birthday, $check->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);
......
...@@ -3,110 +3,128 @@ ...@@ -3,110 +3,128 @@
class RelationsTest extends PHPUnit_Framework_TestCase { class RelationsTest extends PHPUnit_Framework_TestCase {
public function setUp() { public function setUp() {
User::truncate(); }
Book::truncate();
Item::truncate(); public function tearDown()
Role::truncate(); {
Client::truncate(); User::truncate();
} Book::truncate();
Item::truncate();
public function tearDown() Role::truncate();
{ Client::truncate();
User::truncate(); }
Book::truncate();
Item::truncate(); public function testHasMany()
Role::truncate(); {
Client::truncate(); $author = User::create(array('name' => 'George R. R. Martin'));
} Book::create(array('title' => 'A Game of Thrones', 'author_id' => $author->_id));
Book::create(array('title' => 'A Clash of Kings', 'author_id' => $author->_id));
public function testHasMany()
{ $books = $author->books;
$author = User::create(array('name' => 'George R. R. Martin')); $this->assertEquals(2, count($books));
Book::create(array('title' => 'A Game of Thrones', 'author_id' => $author->_id));
Book::create(array('title' => 'A Clash of Kings', 'author_id' => $author->_id)); $user = User::create(array('name' => 'John Doe'));
Item::create(array('type' => 'knife', 'user_id' => $user->_id));
$books = $author->books; Item::create(array('type' => 'shield', 'user_id' => $user->_id));
$this->assertEquals(2, count($books)); Item::create(array('type' => 'sword', 'user_id' => $user->_id));
Item::create(array('type' => 'bag', 'user_id' => null));
$user = User::create(array('name' => 'John Doe'));
Item::create(array('type' => 'knife', 'user_id' => $user->_id)); $items = $user->items;
Item::create(array('type' => 'shield', 'user_id' => $user->_id)); $this->assertEquals(3, count($items));
Item::create(array('type' => 'sword', 'user_id' => $user->_id)); }
Item::create(array('type' => 'bag', 'user_id' => null));
public function testBelongsTo()
$items = $user->items; {
$this->assertEquals(3, count($items)); $user = User::create(array('name' => 'George R. R. Martin'));
} Book::create(array('title' => 'A Game of Thrones', 'author_id' => $user->_id));
$book = Book::create(array('title' => 'A Clash of Kings', 'author_id' => $user->_id));
public function testBelongsTo()
{ $author = $book->author;
$user = User::create(array('name' => 'George R. R. Martin')); $this->assertEquals('George R. R. Martin', $author->name);
Book::create(array('title' => 'A Game of Thrones', 'author_id' => $user->_id));
$book = Book::create(array('title' => 'A Clash of Kings', 'author_id' => $user->_id)); $user = User::create(array('name' => 'John Doe'));
$item = Item::create(array('type' => 'sword', 'user_id' => $user->_id));
$author = $book->author;
$this->assertEquals('George R. R. Martin', $author->name); $owner = $item->user;
$this->assertEquals('John Doe', $owner->name);
$user = User::create(array('name' => 'John Doe')); }
$item = Item::create(array('type' => 'sword', 'user_id' => $user->_id));
public function testHasOne()
$owner = $item->user; {
$this->assertEquals('John Doe', $owner->name); $user = User::create(array('name' => 'John Doe'));
} Role::create(array('type' => 'admin', 'user_id' => $user->_id));
public function testHasOne() $role = $user->role;
{ $this->assertEquals('admin', $role->type);
$user = User::create(array('name' => 'John Doe')); }
Role::create(array('type' => 'admin', 'user_id' => $user->_id));
public function testWithBelongsTo()
$role = $user->role; {
$this->assertEquals('admin', $role->type); $user = User::create(array('name' => 'John Doe'));
} Item::create(array('type' => 'knife', 'user_id' => $user->_id));
Item::create(array('type' => 'shield', 'user_id' => $user->_id));
public function testWithBelongsTo() Item::create(array('type' => 'sword', 'user_id' => $user->_id));
{ Item::create(array('type' => 'bag', 'user_id' => null));
$user = User::create(array('name' => 'John Doe'));
Item::create(array('type' => 'knife', 'user_id' => $user->_id)); $items = Item::with('user')->get();
Item::create(array('type' => 'shield', 'user_id' => $user->_id));
Item::create(array('type' => 'sword', 'user_id' => $user->_id)); $user = $items[0]->getRelation('user');
Item::create(array('type' => 'bag', 'user_id' => null)); $this->assertInstanceOf('User', $user);
$this->assertEquals('John Doe', $user->name);
$items = Item::with('user')->get(); $this->assertEquals(1, count($items[0]->getRelations()));
$this->assertEquals(null, $items[3]->getRelation('user'));
$user = $items[0]->getRelation('user'); }
$this->assertInstanceOf('User', $user);
$this->assertEquals('John Doe', $user->name); public function testWithHashMany()
$this->assertEquals(1, count($items[0]->getRelations())); {
$this->assertEquals(null, $items[3]->getRelation('user')); $user = User::create(array('name' => 'John Doe'));
} Item::create(array('type' => 'knife', 'user_id' => $user->_id));
Item::create(array('type' => 'shield', 'user_id' => $user->_id));
public function testWithHashMany() Item::create(array('type' => 'sword', 'user_id' => $user->_id));
{ Item::create(array('type' => 'bag', 'user_id' => null));
$user = User::create(array('name' => 'John Doe'));
Item::create(array('type' => 'knife', 'user_id' => $user->_id)); $user = User::with('items')->find($user->_id);
Item::create(array('type' => 'shield', 'user_id' => $user->_id));
Item::create(array('type' => 'sword', 'user_id' => $user->_id)); $items = $user->getRelation('items');
Item::create(array('type' => 'bag', 'user_id' => null)); $this->assertEquals(3, count($items));
$this->assertInstanceOf('Item', $items[0]);
$user = User::with('items')->find($user->_id); }
$items = $user->getRelation('items'); public function testWithHasOne()
$this->assertEquals(3, count($items)); {
$this->assertInstanceOf('Item', $items[0]); $user = User::create(array('name' => 'John Doe'));
} Role::create(array('type' => 'admin', 'user_id' => $user->_id));
Role::create(array('type' => 'guest', 'user_id' => $user->_id));
public function testWithHasOne()
{ $user = User::with('role')->find($user->_id);
$user = User::create(array('name' => 'John Doe'));
Role::create(array('type' => 'admin', 'user_id' => $user->_id)); $role = $user->getRelation('role');
Role::create(array('type' => 'guest', 'user_id' => $user->_id)); $this->assertInstanceOf('Role', $role);
$this->assertEquals('admin', $role->type);
$user = User::with('role')->find($user->_id); }
$role = $user->getRelation('role'); public function testEasyRelation()
$this->assertInstanceOf('Role', $role); {
$this->assertEquals('admin', $role->type); // 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() public function testHasManyAndBelongsTo()
{ {
...@@ -175,7 +193,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -175,7 +193,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Client::create(array('name' => 'Boloni Ltd.'))->_id, Client::create(array('name' => 'Boloni Ltd.'))->_id,
Client::create(array('name' => 'Meatballs Inc.'))->_id Client::create(array('name' => 'Meatballs Inc.'))->_id
); );
// Sync multiple records
$user->clients()->sync($clients); $user->clients()->sync($clients);
$user = User::with('clients')->find($user->_id); $user = User::with('clients')->find($user->_id);
......
...@@ -8,7 +8,9 @@ use Illuminate\Auth\Reminders\RemindableInterface; ...@@ -8,7 +8,9 @@ use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, 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()
...@@ -60,5 +62,4 @@ class User extends Eloquent implements UserInterface, RemindableInterface { ...@@ -60,5 +62,4 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
{ {
return $this->email; 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