IsraelOrtuno / Eavquent

EAV modeling package for Laravel and Eloquent – This package is abandoned, please use this fork https://github.com/rinvex/attributes
63 stars 13 forks source link

Closures in RelationBuilder causing error upon model serialization #48

Closed lioreshai closed 6 years ago

lioreshai commented 7 years ago

First of all - this package is great and I've really enjoyed using it and working with it. @IsraelOrtuno thanks a lot!

I'm running into issues caching eloquent results containing models with the Eavquent trait. This is being caused by the presence of closures in RelationBuilder.php

/**
 * Generate the relation closure.
 *
 * @param Model $entity
 * @param Attribute $attribute
 * @return Closure
 */
protected function getRelationClosure(Model $entity, Attribute $attribute)
{
    $method = $this->guessRelationMethod($attribute);

    // This will return a closure fully binded to the current model instance.
    // This will help us to simulate any relation as if it was handly made
    // in the original model class definition using a function statement.
    return Closure::bind(function () use ($entity, $attribute, $method) {
        $relation = $entity->$method($attribute->getModel(), 'entity_id');

        // We add a where clausule in order to fetch only the elements that
        // are related to the given attribute. If no condition is set, it
        // will fetch all the value rows related to the current entity.
        return $relation->where($attribute->getForeignKey(), $attribute->getKey());
    }, $entity, get_class($entity));
}

This results in the error Serialization of 'Closure' is not allowed, because of course serialization of closures isn't allowed in PHP.

This package: https://github.com/jeremeamia/super_closure allows for serializing closures but I'm not sure how it could be worked in here, and what the implications of running database results through eval() are.

Is there some way to tell Eavquent not to boot when querying a model?