Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

why we need to register entities? #168

Closed
singleseeker opened this issue Jan 23, 2022 · 2 comments
Closed

why we need to register entities? #168

singleseeker opened this issue Jan 23, 2022 · 2 comments

Comments

@singleseeker
Copy link

According to the following document, we need to register our entities.I have no idea why we are doing this.

// Push your entity fully qualified namespace
app('rinvex.attributes.entities')->push(\Path\To\Your\Entity::class);

// Or push the morph class alias if any
app('rinvex.attributes.entities')->push('entity');

I found that even the model does not exist, also we could insert the record.

For example, I don't have App\Models\Company, but with the following code, I still can get a record of the company.

app('rinvex.attributes.attribute')->create([
    'slug' => 'size',
    'type' => 'varchar',
    'name' => 'Product Size',
    'entities' => ['App\Models\Company', 'App\Models\User'],
]);
@singleseeker
Copy link
Author

When we get entity attributes, it will check if it is a valid entity.

public function getEntityAttributes(): Collection
    {
        $morphClass = $this->getMorphClass();
        static::$entityAttributes = static::$entityAttributes ?? collect();

        if (! static::$entityAttributes->has($morphClass) && Schema::hasTable(config('rinvex.attributes.tables.attribute_entity'))) {
            $locale = app()->getLocale();

            /* This is a trial to implement per resource attributes,
               it's working but I don't like current implementation.
            $routeParam = request()->route($morphClass);

            // @TODO: This is REALLY REALLY BAD DESIGN!! But can't figure out a better way for now!!
            // Refactor required, we need to catch `$this` itself, we should NOT use request and routes here!!
            // But still at this very early stage, `$this` still not bound to model's data, so it's just empty object!
            $entityId = $routeParam && collect(class_uses_recursive(static::class))->contains(HashidsTrait::class) && ! is_numeric($routeParam)
                ? optional(Hashids::decode($routeParam))[0] : $routeParam;

            $attributes = app('rinvex.attributes.attribute_entity')->where('entity_type', $morphClass)->where('entity_id', $entityId)->get()->pluck('attribute_id');
             */

            $attributes = app('rinvex.attributes.attribute_entity')->where('entity_type', $morphClass)->get()->pluck('attribute_id');
            static::$entityAttributes->put($morphClass, app('rinvex.attributes.attribute')->whereIn('id', $attributes)->orderBy('sort_order', 'ASC')->orderBy("name->\${$locale}", 'ASC')->get()->keyBy('slug'));
        }

        return static::$entityAttributes->get($morphClass) ?? new Collection();
    }

@Omranic
Copy link
Member

Omranic commented Jan 28, 2022

So what's the question exactly?! 🤔

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants