Skip to content

Commit 46c838b

Browse files
PauliusMacernistimgws
authored andcommitted
Fix related to bug #69 (#145)
* I had the same error as described in #69 It is fixed.
1 parent 03aa15d commit 46c838b

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/ElasticquentTrait.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,8 @@ public static function loadRelationsAttributesRecursive(Model $model)
710710
if (method_exists($model, $key)) {
711711
$reflection_method = new ReflectionMethod($model, $key);
712712

713-
if ($reflection_method->class != "Illuminate\Database\Eloquent\Model") {
713+
// Check if method class has or inherits Illuminate\Database\Eloquent\Model
714+
if(!static::isClassInClass("Illuminate\Database\Eloquent\Model", $reflection_method->class)) {
714715
$relation = $model->$key();
715716

716717
if ($relation instanceof Relation) {
@@ -778,4 +779,36 @@ private static function isMultiLevelArray(array $array)
778779
}
779780
return true;
780781
}
782+
783+
/**
784+
* Check the hierarchy of the given class (including the given class itself)
785+
* to find out if the class is part of the other class.
786+
*
787+
* @param string $classNeedle
788+
* @param string $classHaystack
789+
* @return bool
790+
*/
791+
private static function isClassInClass($classNeedle, $classHaystack)
792+
{
793+
// Check for the same
794+
if($classNeedle == $classHaystack) {
795+
return true;
796+
}
797+
798+
// Check for parent
799+
$classHaystackReflected = new \ReflectionClass($classHaystack);
800+
while ($parent = $classHaystackReflected->getParentClass()) {
801+
/**
802+
* @var \ReflectionClass $parent
803+
*/
804+
if($parent->getName() == $classNeedle) {
805+
return true;
806+
}
807+
$classHaystackReflected = $parent;
808+
}
809+
810+
return false;
811+
812+
}
813+
781814
}

0 commit comments

Comments
 (0)