From cf28733b8bfd518d58c515be87669d3d46610ac1 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sun, 14 Dec 2014 14:27:42 +0000 Subject: [PATCH] Prevent getAttribute() from calling static methods --- src/Jenssegers/Mongodb/Model.php | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Jenssegers/Mongodb/Model.php b/src/Jenssegers/Mongodb/Model.php index 95f64920a..de9b4e11f 100644 --- a/src/Jenssegers/Mongodb/Model.php +++ b/src/Jenssegers/Mongodb/Model.php @@ -13,6 +13,7 @@ use DateTime; use MongoId; use MongoDate; +use ReflectionMethod; abstract class Model extends \Jenssegers\Eloquent\Model { @@ -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);