Skip to content

Commit cf28733

Browse files
committed
Prevent getAttribute() from calling static methods
1 parent 3bddd4b commit cf28733

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use DateTime;
1414
use MongoId;
1515
use MongoDate;
16+
use ReflectionMethod;
1617

1718
abstract class Model extends \Jenssegers\Eloquent\Model {
1819

@@ -250,23 +251,32 @@ public function getAttribute($key)
250251
// is handled by the parent method.
251252
if (method_exists($this, $camelKey))
252253
{
253-
$relations = $this->$camelKey();
254+
$method = new ReflectionMethod(get_called_class(), $camelKey);
254255

255-
// This attribute matches an embedsOne or embedsMany relation so we need
256-
// to return the relation results instead of the interal attributes.
257-
if ($relations instanceof EmbedsOneOrMany)
256+
// Ensure the method is not static to avoid conflicting with 'search methods'.
257+
// e.g. find, in, all, where, etc...
258+
if(!$method->isStatic())
258259
{
259-
// If the key already exists in the relationships array, it just means the
260-
// relationship has already been loaded, so we'll just return it out of
261-
// here because there is no need to query within the relations twice.
262-
if (array_key_exists($key, $this->relations))
260+
$relations = $this->$camelKey();
261+
262+
// This attribute matches an embedsOne or embedsMany relation so we need
263+
// to return the relation results instead of the interal attributes.
264+
if ($relations instanceof EmbedsOneOrMany)
263265
{
264-
return $this->relations[$key];
266+
// If the key already exists in the relationships array, it just means the
267+
// relationship has already been loaded, so we'll just return it out of
268+
// here because there is no need to query within the relations twice.
269+
if (array_key_exists($key, $this->relations))
270+
{
271+
return $this->relations[$key];
272+
}
273+
274+
// Get the relation results.
275+
return $this->getRelationshipFromMethod($key, $camelKey);
265276
}
266277

267-
// Get the relation results.
268-
return $this->getRelationshipFromMethod($key, $camelKey);
269278
}
279+
270280
}
271281

272282
return parent::getAttribute($key);

0 commit comments

Comments
 (0)