Case study: model with custom data fields

Hi there,

I am building a CMS with Yii. I am trying to build an "Article" model which should be highly customizable. In order to achieve that, I made an "Article" model belonging to a "Category" model. Category can be a blog post, a lengthy article, a portfolio item, a store product etc., definable by the administrator of the CMS. So the admin can add custom fields to each category, which appear to the editor and the end user.

This is my partial solution, with pros and cons:

Along with some fixed fields such as title and content in the Article model, I built two more related models: a CustomField model, belonging to Category (Category has many CustomField’s) and a CustomData model, belonging to CustomField (CustomField has many CustomData) and to Article (Article has many CustomData).

Each existing custom data unit can be identified in relation to the article id.

Each custom field in an article can be retrieved from the Category->CustomField relationship.

The CMS admin can specify name, data type, max length, etc. for each field.

The CustomData model has several fields for the different data types: value_bool, value_int, value_float, value_varchar, etc. Only one of them should be filled in, according to the data type defined in the related CustomField.

I have had my doubts on this approach, especially having to specify so many fields for the data unit. I am no MySQL guru but I think using a TEXT field to accommodate all data types would be more wasteful than having several specific fields.

Another solution I thought of was to get rid of the CustomData model and store all the custom data in a JSON string inside the Article model, but that would give me less structured data and might degrade search functionality.

I’m not looking for a plug-in. I am learning Yii and would like to implement this myself.

what are the Community’s thoughts on this? Has anybody else run into a similar case?

Thanks, and sorry for the lack of technical language. The source code is interwoven with other functionalities and I was afraid it might be confusing, but I can provide some if needed.

gm

I am not sure if I completely understand what you want to achieve exactly, but because you need highly customized table data(keys and values), you should might think about EAV models(http://en.wikipedia.org/wiki/Entity-attribute-value_model). It can be slower in some cases, but that is only way I can think of.

You can find EAV extension for Yii here: http://code.google.com/p/yiiext/downloads/list

Thanks for the tip. It is useful to know this pattern, although I don’t need such a wide and complex set of data. I can see that this extension relies on a table that stores all attribute values in a TEXT field, which might not be convenient as I wrote before (someone can confirm that?).

What I am basically trying to implement is a model that the website administrator can extend with some extra attributes: e.g. an Article belonging to "reports" Category, beside having its default title, content, creation date etc. fields in its "article" table, can have a "location" (string) attribute, an "archived" (bool) attribute, a "distance" (int) attribute defined in its category only, and applicable to that type of article only.