M
M
magary42015-04-06 21:02:57
Database design
magary4, 2015-04-06 21:02:57

EAV alternative?

I just came up with an idea, and immediately decided to offer it for criticism here, let's
take a classic example with products and their parameters
, let's say it suits us that there will be up to 15 parameters specific to the product type.
what if we create 15 extra1, extra2, ... extra15 fields in the table
and already in the business logic to implement the consistency of the fields of the database to the properties of the object.

public function getProducts() {
  $items = query ( ..... );
  
  foreach($items as $item) {
    switch($item->type) {
      case "tv":
         $product = new Product();
         $product->matrix = $item['extra1'];
         $product->remoteControl = $item['extra2'];       
         .................
       break;
       case "dvd":
         $product = new Product();
         $product->format = $item['extra1'];
         $product->voltage = $item['extra2'];       
         .................
        break;
     }
  }

}

well, and implement searches by analogy, i.e. if we are looking for a freezer with 5 drawers, then we describe what we need to look for an entry where extra12=5

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Shikanov, 2015-04-06
@dizballanze

this is a crutch, and not an alternative, for a long time in normal DBMS, like PostgreSQL, there are data types like an array and a hash table

I
Igor Kalashnikov, 2015-04-07
@zo0m

This is quite a normal approach. I met this (param1, param2) even in large enterprise solutions, because it is not always possible to effectively execute an alter table to add a field, and (or) your case, when there is a set of attributes that are characteristic of a part of entities and do not intersect.
Only mapping for each entity I would take out separately:

var productToExtraMap = {
  'tv'  : {
   matrix :  'extra_1',
   remoteControl : 'extra_2'
   },
  'dvd' : { 
    ...
   }
}

and on such a map to collect objects.
Then you will have 1 place where the object is going and 1 place where the mapping is declared. There will be no unnecessary nested ifs, etc.
P.S. Mapu can be deployed in another way, this is already as you prefer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question