2

I have an application that allows for "contacts" to be made completely customized. My method of doing that is letting the administrator setup all of the fields allowed for the contact. My database is as follows:

Contacts

  • id
  • active
  • lastactive
  • created_on

Fields

  • id
  • label

FieldValues

  • id
  • fieldid
  • contactid
  • response

So the contact table only tells whether they are active and their identifier; the fields tables only holds the label of the field and identifier, and the fieldvalues table is what actually holds the data for contacts (name, address, etc.)

So this setup has worked just fine for me up until now. The client would like to be able to pull a cumulative report, but say state of all the contacts in a certain city. Effectively the data would have to look like the following

California (from fields table)

  • Costa Mesa - (from fields table) 5 - (counted in fieldvalues table)
  • Newport 2

Connecticut

  • Wallingford 2
  • Clinton 2
  • Berlin 5

The state field might be id 6 and the city field might be id 4. I don't know if I have just been looking at this code way to long to figure it out or what,

The SQL to create those three tables can be found at https://s3.amazonaws.com/davejlong/Contact.sql

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Dave Long
  • 9,569
  • 14
  • 59
  • 89

1 Answers1

0

You've got an Entity Attribute Value (EAV) model. Use the field and fieldvalue tables for searching only - the WHERE caluse. Then make life easier by keeping the full entity's data in a CLOB off the main table (e.g. Contacts.data) in a serialized format (WDDX is good for this). Read the data column out, deserialize, and work with on the server side. This is much easier than the myriad of joins you'd need to do otherwise to reproduce the fully hydrated entity from an EAV setup.

orangepips
  • 9,891
  • 6
  • 33
  • 57
  • that wasn't what I am going to go with, but that would be the best solution to the problem. I did figure out a way of hacking things together to get the job done until I can overhaul the database. I just don't have time to right now. I will not post my answer as it is, as I said, hacking things together. – Dave Long Mar 07 '11 at 15:37