Skip to content

Commit

Permalink
Replaced method_exists() with is_callable() (elasticquent#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thedigit authored and timgws committed Apr 11, 2016
1 parent 2c88375 commit 404e791
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/ElasticquentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Elasticquent;

use Exception;
use ReflectionMethod;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;

Expand Down Expand Up @@ -685,25 +686,27 @@ public static function hydrateRecursive(Model $model, array $items, Relation $pa
* @param \Illuminate\Database\Eloquent\Model $model
*/
public static function loadRelationsAttributesRecursive(Model $model)
{
$attributes = $model->getAttributes();

foreach ($attributes as $key => $value) {
if (method_exists($model, $key)) {
$relation = $model->$key();
if ($relation instanceof Relation) {
// Check if the relation field is single model or collections
if (!static::isMultiLevelArray($value)) {
$value = [$value];
}
$models = static::hydrateRecursive($relation->getModel(), $value, $relation);
// Unset attribute before match relation
unset($model[$key]);
$relation->match([$model], $models, $key);
}
{
$attributes = $model->getAttributes();
foreach ($attributes as $key => $value) {
if (method_exists($model, $key) ) {
$refl = new ReflectionMethod($model, $key);
if($refl->class != "Illuminate\Database\Eloquent\Model"){
$relation = $model->$key();
if ($relation instanceof Relation) {
// Check if the relation field is single model or collections
if (!static::isMultiLevelArray($value)) {
$value = [$value];
}
$models = static::hydrateRecursive($relation->getModel(), $value, $relation);
// Unset attribute before match relation
unset($model[$key]);
$relation->match([$model], $models, $key);
}
}
}
}
}

/**
* Get the pivot attribute from a model.
Expand Down

0 comments on commit 404e791

Please sign in to comment.