PHPackages                             ttp/zf2-eav - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ttp/zf2-eav

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ttp/zf2-eav
===========

Zend Framework 2 EAV

626.8k↓40.6%3PHP

Since Aug 31Pushed 12y ago4 watchersCompare

[ Source](https://github.com/ttp/zf2-eav)[ Packagist](https://packagist.org/packages/ttp/zf2-eav)[ RSS](/packages/ttp-zf2-eav/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Zend Frawemork 2 EAV
====================

[](#zend-frawemork-2-eav)

Zend Framework Entity-Attribute-Value library.

[What is EAV?](http://en.wikipedia.org/wiki/Entity-attribute-value_model)

Quick start
-----------

[](#quick-start)

### Prepare tables

[](#prepare-tables)

For example if you have *products* table and you want to save EAV attributes for this table. Then create several tables where EAV attributes will be stored. Example:

- products\_int,
- products\_decimal
- products\_string
- products\_text

Each table should contain next fields:

- id(INT) – primary key(auto increment)
- entity\_id(INT) – identifier of your record in the products table
- attribute\_id(INT) – identifier of attribute
- value – in this field the value of attribute will be stored. Type of this field should be different for each table(int, decimal, varchar, text)

Also you should have table of attributes with next fields inside:

- id, primary key
- type, type of attribute
- name, name of attribute

The names of these fields do not necessarily have to be called as above. Here we have the names of fields by default. If you don't have such table then create it.

Then create several records in the `attributes` table

```
|  *attribute_id* | *attribute_type*  | *attribute_name* |
| --------------- | ----------------- |------------------|
| 1               | int               | quantity         |
| 2               | string            | title            |
| 3               | text              | description      |

```

### Extends Eav

[](#extends-eav)

You can specify the names of required fields:

```
use Eav\Eav;

class EavProduct extends Eav
{
    protected $_entitiesTableFieldId = 'id'; // name of primary key of products table

    protected $_attributesTableName = 'attributes'; // name of 'attributes' table
    protected $_attributesTableFieldId   = 'attribute_id'; // name of primary key of attributes table
    protected $_attributesTableFieldType = 'attribute_type'; // field where attribute type is stored
    protected $_attributesTableFieldName = 'attribute_name'; // field where attribute name is stored
}
```

### Save attributes

[](#save-attributes)

```
use Zend\Db\TableGateway\TableGateway;

$productsTable = new TableGateway('products', $adapter);
$eav = new EavProduct($productsTable);

$products = $productsTable->select(array('id' => array(1,2,3)));

foreach ($products as $product) {
    $eav->setAttributeValue($product, 'quantity', 10);
    $eav->setAttributeValue($product, 'title', 'product title');
    $eav->setAttributeValue($product, 'description', 'my description');
}
```

### Get attributes

[](#get-attributes)

```
$productsTable = new TableGateway('products');
$eav = new EavProduct($productsTable);

$product = $productsTable->select(array('id' => 1))->current();
echo $eav->getAttributeValue($product, 'quantity');
echo $eav->getAttributeValue($product, 'title');
echo $eav->getAttributeValue($product, 'description');

// or using attribute id
echo $eav->getAttributeValue($product, '1');

// or using attribute object(Row)
$attributesTable = $eav->getAttributesTable();
$attribute = $attributeTable->select(array('id' => 2))->current();
echo $eav->getAttributeValue($product, $attribute);

// or using eav object
$attribute = $eav->getAttribute('title');
echo $eav->getAttributeValue($product, $attribute);
```

Full example
------------

[](#full-example)

Controller:

```
$productsTable = new TableGateway('products', $adapter);
$eav = new EavProduct($productsTable);
$attributesTable = $eav->getAttributesTable();

$products = $productsTable->select(array('id' => array(1,2,3)));
$attributes = $attributesTable->select();
```

View:

```

        :

```

Speed up
--------

[](#speed-up)

With the approach above, every time you want to get the value of attribute you will have query to the database. You can get the attribute values using only one query to the database. Below are some examples:

```
$productsTable = new TableGateway('products', $adapter);
$eav = new EavProduct($productsTable);
$attributesTable = $eav->getAttributesTable();

$products = $productsTable->select(array('id' => array(1,2,3)));

// Loading all attributes
$attributes = $attributesTable->select();
$cache = $eav->loadAttributes($products, $attributes);

foreach ($products as $product) {
    // no more queries
    echo $eav->getAttributeValue($product, 'title', $cache);
    echo $eav->getAttributeValue($product, 'description', $cache);
    echo $eav->getAttributeValue($product, 'quantity', $cache);
}

// Loading some attributes
$where = array("name" => array('title', 'description'))
$attributes = $attributesTable->select($where);

$cache = $eav->loadAttributes($products, $attributes);

foreach ($products as $product) {
    // no more queries
    echo $eav->getAttributeValue($product, 'title', $cache);
    echo $eav->getAttributeValue($product, 'description', $cache);

    // here we have query to database since this attribute was not cached
    echo $eav->getAttributeValue($product, 'quantity', $cache);
}

$cachedValues = $cache->toArray(); // returns array('entity_id' => array('attribute_id' => 'value'))
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/d24444ee70fed5ca6d173425c7b6f8a8bfaa1c5be345f4799fe800cf983bdd87?d=identicon)[ttp](/maintainers/ttp)

---

Top Contributors

[![ttp](https://avatars.githubusercontent.com/u/52537?v=4)](https://github.com/ttp "ttp (10 commits)")

### Embed Badge

![Health badge](/badges/ttp-zf2-eav/health.svg)

```
[![Health](https://phpackages.com/badges/ttp-zf2-eav/health.svg)](https://phpackages.com/packages/ttp-zf2-eav)
```

###  Alternatives

[franmomu/silex-pagerfanta-provider

Silex ServiceProvider for Pagerfanta Library

1312.1k](/packages/franmomu-silex-pagerfanta-provider)[bostondv/bootstrap-ninja-forms

Adds Bootstrap classes to Ninja Forms

222.2k](/packages/bostondv-bootstrap-ninja-forms)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
