|
1 | 1 | `Entity-Attribute-Value (EAV)`__
|
2 | 2 | ================================
|
3 | 3 |
|
| 4 | +The Entity–attribute–value (EAV) pattern in order to implement EAV model with PHP. |
| 5 | + |
4 | 6 | Purpose
|
5 | 7 | -------
|
6 | 8 |
|
7 |
| -... |
| 9 | +The Entity–attribute–value (EAV) model is a data model to describe entities |
| 10 | +where the number of attributes (properties, parameters) that can be used |
| 11 | +to describe them is potentially vast, but the number that will actually apply |
| 12 | +to a given entity is relatively modest. |
8 | 13 |
|
9 | 14 | Examples
|
10 | 15 | --------
|
11 | 16 |
|
12 |
| -``` |
13 |
| -use DesignPatterns\More\EAV\Entity; |
14 |
| -use DesignPatterns\More\EAV\Attribute; |
15 |
| -use DesignPatterns\More\EAV\Value; |
16 |
| - |
17 |
| -// color attribute |
18 |
| -$color = (new Attribute())->setName('Color'); |
19 |
| -// color values |
20 |
| -$colorSilver = (new Value($color))->setName('Silver'); |
21 |
| -$colorGold = (new Value($color))->setName('Gold'); |
22 |
| -$colorSpaceGrey = (new Value($color))->setName('Space Grey'); |
23 |
| - |
24 |
| -// memory attribute |
25 |
| -$memory = (new Attribute())->setName('Memory'); |
26 |
| -// memory values |
27 |
| -$memory4Gb = (new Value($memory))->setName('4GB'); |
28 |
| -$memory8Gb = (new Value($memory))->setName('8GB'); |
29 |
| -$memory16Gb = (new Value($memory))->setName('16GB'); |
30 |
| - |
31 |
| -// storage attribute |
32 |
| -$storage = (new Attribute())->setName('Storage'); |
33 |
| -// storage values |
34 |
| -$storage128Gb = (new Value($storage))->setName('128GB'); |
35 |
| -$storage256Gb = (new Value($storage))->setName('256GB'); |
36 |
| -$storage512Gb = (new Value($storage))->setName('512GB'); |
37 |
| -$storage1Tb = (new Value($storage))->setName('1TB'); |
38 |
| - |
39 |
| -// entities |
40 |
| -$mac = (new Entity()) |
41 |
| - ->setName('MacBook') |
42 |
| - // colors |
43 |
| - ->addValue($colorSilver) |
44 |
| - ->addValue($colorGold) |
45 |
| - ->addValue($colorSpaceGrey) |
46 |
| - // memories |
47 |
| - ->addValue($memory8Gb) |
48 |
| - // storages |
49 |
| - ->addValue($storage256Gb) |
50 |
| - ->addValue($storage512Gb) |
51 |
| -; |
52 |
| - |
53 |
| -$macAir = (new Entity()) |
54 |
| - ->setName('MacBook Air') |
55 |
| - // colors |
56 |
| - ->addValue($colorSilver) |
57 |
| - // memories |
58 |
| - ->addValue($memory4Gb) |
59 |
| - ->addValue($memory8Gb) |
60 |
| - // storages |
61 |
| - ->addValue($storage128Gb) |
62 |
| - ->addValue($storage256Gb) |
63 |
| - ->addValue($storage512Gb) |
64 |
| -; |
65 |
| - |
66 |
| -$macPro = (new Entity()) |
67 |
| - ->setName('MacBook Pro') |
68 |
| - // colors |
69 |
| - ->addValue($colorSilver) |
70 |
| - // memories |
71 |
| - ->addValue($memory8Gb) |
72 |
| - ->addValue($memory16Gb) |
73 |
| - // storages |
74 |
| - ->addValue($storage128Gb) |
75 |
| - ->addValue($storage256Gb) |
76 |
| - ->addValue($storage512Gb) |
77 |
| - ->addValue($storage1Tb) |
78 |
| -; |
79 |
| -``` |
| 17 | +Check full work example in `example.php`_ file. |
| 18 | + |
| 19 | +.. code-block:: php |
| 20 | +
|
| 21 | + use DesignPatterns\More\EAV\Entity; |
| 22 | + use DesignPatterns\More\EAV\Attribute; |
| 23 | + use DesignPatterns\More\EAV\Value; |
| 24 | +
|
| 25 | + // Create color attribute |
| 26 | + $color = (new Attribute())->setName('Color'); |
| 27 | + // Create color values |
| 28 | + $colorSilver = (new Value($color))->setName('Silver'); |
| 29 | + $colorGold = (new Value($color))->setName('Gold'); |
| 30 | + $colorSpaceGrey = (new Value($color))->setName('Space Grey'); |
| 31 | +
|
| 32 | + // Create memory attribute |
| 33 | + $memory = (new Attribute())->setName('Memory'); |
| 34 | + // Create memory values |
| 35 | + $memory4Gb = (new Value($memory))->setName('4GB'); |
| 36 | + $memory8Gb = (new Value($memory))->setName('8GB'); |
| 37 | + $memory16Gb = (new Value($memory))->setName('16GB'); |
| 38 | +
|
| 39 | + // Create storage attribute |
| 40 | + $storage = (new Attribute())->setName('Storage'); |
| 41 | + // Create storage values |
| 42 | + $storage128Gb = (new Value($storage))->setName('128GB'); |
| 43 | + $storage256Gb = (new Value($storage))->setName('256GB'); |
| 44 | + $storage512Gb = (new Value($storage))->setName('512GB'); |
| 45 | + $storage1Tb = (new Value($storage))->setName('1TB'); |
| 46 | +
|
| 47 | + // Create entities with specific values |
| 48 | + $mac = (new Entity()) |
| 49 | + ->setName('MacBook') |
| 50 | + // colors |
| 51 | + ->addValue($colorSilver) |
| 52 | + ->addValue($colorGold) |
| 53 | + ->addValue($colorSpaceGrey) |
| 54 | + // memories |
| 55 | + ->addValue($memory8Gb) |
| 56 | + // storages |
| 57 | + ->addValue($storage256Gb) |
| 58 | + ->addValue($storage512Gb) |
| 59 | + ; |
| 60 | +
|
| 61 | + $macAir = (new Entity()) |
| 62 | + ->setName('MacBook Air') |
| 63 | + // colors |
| 64 | + ->addValue($colorSilver) |
| 65 | + // memories |
| 66 | + ->addValue($memory4Gb) |
| 67 | + ->addValue($memory8Gb) |
| 68 | + // storages |
| 69 | + ->addValue($storage128Gb) |
| 70 | + ->addValue($storage256Gb) |
| 71 | + ->addValue($storage512Gb) |
| 72 | + ; |
| 73 | +
|
| 74 | + $macPro = (new Entity()) |
| 75 | + ->setName('MacBook Pro') |
| 76 | + // colors |
| 77 | + ->addValue($colorSilver) |
| 78 | + // memories |
| 79 | + ->addValue($memory8Gb) |
| 80 | + ->addValue($memory16Gb) |
| 81 | + // storages |
| 82 | + ->addValue($storage128Gb) |
| 83 | + ->addValue($storage256Gb) |
| 84 | + ->addValue($storage512Gb) |
| 85 | + ->addValue($storage1Tb) |
| 86 | + ; |
| 87 | +
|
80 | 88 |
|
81 | 89 | UML Diagram
|
82 | 90 | -----------
|
83 | 91 |
|
84 |
| -... |
| 92 | +.. image:: uml/uml.png |
| 93 | + :alt: EAV UML Diagram |
| 94 | + :align: center |
85 | 95 |
|
86 | 96 | Code
|
87 | 97 | ----
|
@@ -110,5 +120,6 @@ Tests/ValueTest.php
|
110 | 120 | :linenos:
|
111 | 121 |
|
112 | 122 |
|
| 123 | +.. _`example.php`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/EAV/example.php |
113 | 124 | .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/EAV
|
114 | 125 | .. __: https://en.wikipedia.org/wiki/Entity–attribute–value_model
|
0 commit comments