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

Troubles Seeding Attributes on Entity #44

Closed dhildreth closed 3 years ago

dhildreth commented 6 years ago

Seeing some really strange activity when attempting to seed attributes. I've been able to get it to work, but only when the stars are in alignment and I do the hokey pokey exactly five times while whistling yankie doodle dandy. For the time when running php artisan db:seed was successful, I ran it again immediately without making any other changes and it failed the second time. I can't get it to work anymore. Strange part is, I can get the attributes to save just fine when using php artisan tinker. I'm not sure what's happening other than a potential race condition and/or caching issue.

So, here we start by clearing the cache and then running php artisan db:seed (it does the same thing for php artisan migrate:fresh --seed):

vagrant@homestead:~/Code/app.test$ php artisan cache:clear
Cache cleared successfully.
vagrant@homestead:~/Code/app.test$ php artisan db:seed

...

Seeding: ProductsTableSeeder
Seeding: AttributeSeeder
Seeding: ProductAttributeSeeder

In Connection.php line 664:

  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cpu-speed' in 'field list' (SQL: update `products` set `cpu-speed` = 1660, `updated_at` = 2018-05-30 14:42:30 where `id` = 14)  

In Connection.php line 479:

  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cpu-speed' in 'field list'  

Okay, so we see we have an error when attempting to seed the DB. Let's see if it works in php artisan tinker:

vagrant@homestead:~/Code/app.test$ php artisan tinker
Psy Shell v0.9.3 (PHP 7.2.0-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> $product = App\Product::find(14)
=> App\Product {#2609
     id: 14,

     ...

     created_at: "2018-05-30 14:42:30",
     updated_at: "2018-05-30 14:42:30",
     cpu-speed: null,
   }
>>> $product->{'cpu-speed'} = '1000'
=> "1000"
>>> $product->save()
=> true
>>> $product
=> App\Product {#2609
     id: 14,

     ...

     created_at: "2018-05-30 14:42:30",
     updated_at: "2018-05-30 14:42:30",
     cpu-speed: Rinvex\Attributes\Models\Type\Integer {#2619
       entity_id: 14,
       entity_type: "product",
       attribute_id: 1,
       content: "1000",
       updated_at: "2018-05-30 14:48:47",
       created_at: "2018-05-30 14:48:47",
       id: 1,
     }
   }
>>> 

Yup, it works here. Not sure what's happening with the seeding part. Any ideas?

DatabaseSeeder.php

        $this->call(ProductsTableSeeder::class);
        $this->call(AttributeSeeder::class);
        $this->call(ProductAttributeSeeder::class);

AttributeSeeder.php

use Rinvex\Attributes\Models\Attribute;

        Attribute::create([
            'type' => 'integer',
            'name' => 'CPU Speed',
            'entities' => ['product'],
            //'group' => 'CPU',
        ]);

ProductAttributeSeeder.php

    public function run()
    {
        $product = Product::find(14);
        $product->{'cpu-speed'} = "1666";
        $product->save();
    }

App\Product.php

use Rinvex\Attributes\Traits\Attributable;

class Product extends Model implements HasMedia
{
    use HasMediaTrait;
    use Attributable;
    // Eager loading all the registered attributes
    protected $with = ['eav'];

    ...
IsraelOrtuno commented 6 years ago

Eager loading seems to have issues at this point due to new Eloquent changes, so please avoid using it for now until we come up with a solution.

For the attribute slug, update to the latest version, all slugs are forced to be snake_case and that would probably fix your issue.

dhildreth commented 6 years ago

Thank you for taking a look into it!

dhildreth commented 6 years ago

Any updates on this one? I'd love to be able to seed attributes for staging and testing.

Omranic commented 3 years ago

Thank you for your patience, I did a complete rewrite for the whole package and changed how collections & relationships work. 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! 🙂