Skip to content

Commit

Permalink
Merge #383
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed May 25, 2015
1 parent e0a3cda commit e2bc57c
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/Jenssegers/Mongodb/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use MongoId;
use MongoDate;
use Carbon\Carbon;
use ReflectionMethod;
use Illuminate\Database\Eloquent\Relations\Relation;
use Jenssegers\Mongodb\Eloquent\Builder;
use Jenssegers\Mongodb\Relations\EmbedsOneOrMany;
Expand Down Expand Up @@ -233,22 +234,28 @@ public function getAttribute($key)
// is handled by the parent method.
if (method_exists($this, $camelKey))
{
$relations = $this->$camelKey();
$method = new ReflectionMethod(get_called_class(), $camelKey);

// This attribute matches an embedsOne or embedsMany relation so we need
// to return the relation results instead of the interal attributes.
if ($relations instanceof EmbedsOneOrMany)
// Ensure the method is not static to avoid conflicting with Eloquent methods.
if ( ! $method->isStatic())
{
// 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))
$relations = $this->$camelKey();

// This attribute matches an embedsOne or embedsMany relation so we need
// to return the relation results instead of the interal attributes.
if ($relations instanceof EmbedsOneOrMany)
{
return $this->relations[$key];
// 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);
}

// Get the relation results.
return $this->getRelationshipFromMethod($key, $camelKey);
}
}

Expand Down

0 comments on commit e2bc57c

Please sign in to comment.