Commit 0add21c3 authored by Pooya Parsa's avatar Pooya Parsa

model relations

parent 72cd1f15
...@@ -8,7 +8,6 @@ use Jenssegers\Mongodb\Query\Builder as QueryBuilder; ...@@ -8,7 +8,6 @@ use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
use Jenssegers\Mongodb\Relations\EmbedsMany; use Jenssegers\Mongodb\Relations\EmbedsMany;
use Jenssegers\Mongodb\Relations\EmbedsOne; use Jenssegers\Mongodb\Relations\EmbedsOne;
use Jenssegers\Mongodb\Relations\EmbedsOneOrMany; use Jenssegers\Mongodb\Relations\EmbedsOneOrMany;
use Jenssegers\Mongodb\Relations\MorphTo;
use MongoDB\BSON\ObjectID; use MongoDB\BSON\ObjectID;
use MongoDB\BSON\UTCDateTime; use MongoDB\BSON\UTCDateTime;
use ReflectionMethod; use ReflectionMethod;
...@@ -234,7 +233,19 @@ abstract class Model extends BaseModel ...@@ -234,7 +233,19 @@ abstract class Model extends BaseModel
// This attribute matches an embedsOne or embedsMany relation so we need // This attribute matches an embedsOne or embedsMany relation so we need
// to return the relation results instead of the interal attributes. // to return the relation results instead of the interal attributes.
if ($relations instanceof EmbedsOneOrMany or $relations instanceof MorphTo) { if ($relations instanceof EmbedsOneOrMany) {
// If the key already exists in the relationships array, it just means the
// relationship has already been loaded, so we'll just return it out of
// here because there is no need to query within the relations twice.
if (array_key_exists($key, $this->relations)) {
return $this->relations[$key];
}
// Get the relation results.
return $this->getRelationshipFromMethod($key, $camelKey);
}
if ($relations instanceof Relation) {
// If the key already exists in the relationships array, it just means the // If the key already exists in the relationships array, it just means the
// relationship has already been loaded, so we'll just return it out of // relationship has already been loaded, so we'll just return it out of
// here because there is no need to query within the relations twice. // here because there is no need to query within the relations twice.
......
...@@ -322,8 +322,8 @@ class RelationsTest extends TestCase ...@@ -322,8 +322,8 @@ class RelationsTest extends TestCase
$this->assertTrue(array_key_exists('groups', $user->getAttributes())); $this->assertTrue(array_key_exists('groups', $user->getAttributes()));
// Assert they are attached // Assert they are attached
$this->assertTrue(in_array($group->_id, $user->groups)); $this->assertTrue(in_array($group->_id, $user->groups->pluck('_id')->toArray()));
$this->assertTrue(in_array($user->_id, $group->users)); $this->assertTrue(in_array($user->_id, $group->users->pluck('_id')->toArray()));
$this->assertEquals($group->_id, $user->groups()->first()->_id); $this->assertEquals($group->_id, $user->groups()->first()->_id);
$this->assertEquals($user->_id, $group->users()->first()->_id); $this->assertEquals($user->_id, $group->users()->first()->_id);
} }
......
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