Skip to content

Prevent getAttribute() from calling static methods #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/Jenssegers/Mongodb/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use DateTime;
use MongoId;
use MongoDate;
use ReflectionMethod;

abstract class Model extends \Jenssegers\Eloquent\Model {

Expand Down Expand Up @@ -250,23 +251,32 @@ 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 'search methods'.
// e.g. find, in, all, where, etc...
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);
}

}

return parent::getAttribute($key);
Expand Down