Not sure if this is the best structure for my database but this is what I have at the moment (open to suggestions for alterations). I basically want to create pages that have a user defined set of fields for each page. So I have four tables: pages, page_fields, page_data and data_fields.
pages:
id name
1 home
page_fields:
page_id field_id
1 1
1 2
1 3
page_data:
page_id field_id content
1 1 'This is the Title'
1 3 'Some description here'
data_fields:
id field_name field_type
1 title text
2 subtitle text
3 description textarea
First of all is this a good database structure?
Secondly, how can I get the required fields as defined by the page_field table, the data (if there is any) for that field and the filed_name and filed_type given that we know the page id?
I presume there will be a couple of joins here but I just can’t work them out.
Any help appreciated.
Assuming that one field on one page can have single value, you can easily eliminate
page_fieldstable. List of fields assigned to the page will be stored inpage_dataand if there is no content yet, NULL value will be in thecontentcolumn.Something like
SELECT * FROM page_fields INNER JOIN page_data ON (page_fields.page_id = page_data.page_id) INNER JOIN data_fields ON (page_data.field_id = data_fields.id);Additionally read about Entity Attribute Value model, may be it will generate some new ideas and generally useful for understanding things: http://en.wikipedia.org/wiki/Entity-attribute-value_model