rinvex / laravel-attributes

⚠️ [ABANDONED] Rinvex Attributable is a robust, intelligent, and integrated Entity-Attribute-Value model (EAV) implementation for Laravel Eloquent, with powerful underlying for managing entity attributes implicitly as relations with ease. It utilizes the power of Laravel Eloquent, with smooth and seamless integration.
MIT License
434 stars 104 forks source link

Cannot get newly added attribute while queue is running #127

Closed donamkhanh closed 3 years ago

donamkhanh commented 4 years ago

Firstly, thank you for great package 👍

I'm using this package on my project, everything work fine except the case when user create new their attribute, then trigger an event to update in Laravel queue. It seems we cannot get newly added attribute while queue is running. It shows only old attributes which are added before queue is started.

What should I do in this case? I don't really want to call artisan command queue:restart to get it works.

Best regards

simonworkhouse commented 4 years ago

I have noticed this too and ended up implementing a work-around by extending the Attributable trait so that it's static "cache" could be cleared. The issue is with the static $entityAttributes property in Rinvex\Attributes\Traits\Attributable, and this won't be refreshed if an attribute is created for a model.

My work-around is something like

<?php

namespace Blah\Eav\Traits;

use Rinvex\Attributes\Traits\Attributable as AttributableBase;

trait Attributable
{
    use AttributableBase;

    /**
     * Clear the attributes cache for this model.
     *
     * @return void
     */
    public function clearAttributableCache()
    {
        $morphClass = $this->getMorphClass();
        if (static::$entityAttributes && static::$entityAttributes->has($morphClass)) {
            static::$entityAttributes->forget($morphClass);
        }
    }
}

Use this trait in your models instead, and to clear the "cache" just call the following after creating an attribute

        // Clear the cache for each entity type that is registered for this attribute.
        foreach ($attributeObject->entities as $entity) {
            $entityInstance = app()->make($entity);
            $entityInstance->clearAttributableCache();
        }

Time permitting. I may eventually create a pull request for something to achieve the same functionality.

simonworkhouse commented 4 years ago

@donamkhanh Just an advance warning, I have created a pull request for this (https://github.com/rinvex/laravel-attributes/pull/132) so it might introduce compatibility issues if it is merged and you implemented the changes I suggested previously.

Omranic commented 3 years ago

Thank you for your patience. I did a complete rewrite for the whole package and changed how this feature works. In the new refactor, it actually uses normal relationships, and should be easy and straightforward like default Laravel relationships. Although, that refactor is incomplete.

I'm still not happy with the overall performance (I believe we can reduce number of executed queries), if you want to check it out, see https://github.com/rinvex/laravel-attributes/tree/refactor-to-native-laravel-relationships

Currently no plans to merge that rewrite, but hopefully sometime I can get it to a stable state, improve performance and release it. Any help with that branch would be much appreciated! 🙂