sunel/eav

repository·master·Indexed 41 days ago

https://github.com/sunel/eav

A PHP-based implementation of the Entity-Attribute-Value (EAV) data modeling pattern for Laravel, designed for flexible, schema-less data structures and sparse data. It includes support for custom field types, custom table schemas, faceted search, and an optional EAV Dashboard for management.

Tokens
17.5K
Snippets
76
Records
89
Agent score
98%

What's inside sunel-eav

  1. What is an Attribute Set

    master

    An AttributeSet acts as a template for an entity. It defines which fields are available during data entry and which values are returned when data is retrieved.

    Key characteristics:

    • Default Set: A Default set is created initially.
    • Uniqueness: An AttributeSet is unique for an entity, though an entity can have multiple sets.
    • Creation: A set is automatically created or reused when mapping an attribute to an entity.
    • Relationships:
      • Attribute Sets are attached to an Entity via a hasMany relationship.
      • Attributes are attached to a Set via a hasManyThrough relationship using an intermediate EntityAttribute table.
  2. What is the Entity–attribute–value (EAV) model

    master
    The Entity–attribute–value (EAV) model is a data modeling pattern used to encode entities where the potential number of attributes (properties or parameters) is vast, but the number of attributes actually applied to any single entity is relatively small. This approach is more space-efficient than traditional relational models when dealing with sparse data.
  3. Enable attributes for faceted search

    master
    To make an attribute available for faceted navigation (filtering), you must ensure that its is_filterable property is set to 1. If this is not set, the attribute will not appear in the facets returned by the query builder.
  4. Limitations of the EAV model

    master

    While EAV provides flexibility and space efficiency, it has a significant performance trade-off:

    • Data Fragmentation: Because entity data is split across multiple tables, retrieving a complete entity record requires performing multiple table joins, which can be more computationally expensive than querying a single flat table.
  5. When to use the EAV model

    master

    You should consider using the EAV model in the following scenarios:

    • Sparse Data: When you have many possible attributes, but most objects only use a few. In a traditional table, this results in many empty (NULL) columns. EAV avoids this by only storing values that actually exist.
    • Loose or Evolving Schemas: When your data structure is likely to change over time. EAV allows you to add new attributes vertically (as new rows) without requiring database schema redesigns or structural changes to existing tables.
  6. Model Naming and Table Conventions

    master

    EAV models follow standard Eloquent naming conventions for table names: the table name is the snake_case, plural version of the class name (e.g., Products class uses products table).

    Note on Flat Tables: If Flat Table functionality is enabled, the table name will be prefixed with _flat.

  7. Understand how attribute values are stored

    master

    In this EAV (Entity-Attribute-Value) implementation, the actual data for an attribute is stored in type-specific tables rather than a single generic table. This ensures data integrity and performance by using appropriate database types.

    Values are stored in tables named following the pattern [entity]_ [datatype]. For example, if your entity is product, values will be stored in tables such as:

    • product_varchar
    • product_int
    • product_decimal
    • product_datetime
    • product_text
  8. Use static attributes for performance optimization

    master

    Static attributes are attributes stored directly in the main table of an entity rather than in a separate EAV table. They are always loaded with the entity, making them ideal for frequently accessed data or for optimizing data lookups.

    To implement a static attribute, you must perform two steps in your migration script:

    1. Add a column to the main entity table with the appropriate column definition.
    2. Register the attribute using the Eav\Attribute::add method, setting the 'backend_type' option to 'static'.
    // Example logic for a migration script
    
    // 1. Add column to the main entity table (SQL/Migration logic)
    // e.g., ALTER TABLE entities ADD COLUMN my_static_column VARCHAR(255);
    
    // 2. Register the attribute as static
    Eav\Attribute::add('my_static_column', [
        'backend_type' => 'static',
        // ... other attribute definitions
    ]);
  9. How Attribute Groups work in EAV

    master

    In this EAV implementation, attributes are organized into AttributeGroup entities.

    • Sets and Groups: An AttributeSet contains multiple AttributeGroup entities via a one-to-many relationship. By default, a General group is created initially.
    • Groups and Attributes: AttributeGroup entities are linked to Attribute entities through a many-to-many relationship managed by an EntityAttribute pivot table. This is exposed via a hasManyThrough relationship.

    This hierarchy allows you to categorize attributes logically within a set.

  10. Remove attribute options

    master

    Use Eav\AttributeOption::remove() to delete specific options from an attribute. You must provide the attribute instance (retrieved via Eav\Attribute::findByCode()) and an associative array containing the key-value pairs of the options you wish to remove.

    Eav\AttributeOption::remove(Eav\Attribute::findByCode('size', 'product'), [
        's' => 'Small'
    ]);
  11. Apply EAV migrations

    master

    Once you have generated the entity and attribute migrations (and edited the attribute migration files if necessary), run the standard Laravel migration command to apply the changes to your database.

    $ php artisan migrate