Question

I'm creating a app where user will can create meta field to store data on database, this fields will receive different kinds of data, like dates or monetary values.

My initial idea to create this this fields is store in a database table (eg: meta_fields), the name, value and validations of this field.

After on the time of use it on app, I intend to extend the AR methods __get and __set to use the name field as a attribute.

Example:

If on my table meta_fields i have this values:

----------------------------------------------------
| id | name     | value      | validation          |
----------------------------------------------------
| 1  | money_en | $562,00    | some validation...  |
----------------------------------------------------
| 2  | date_crt | 2010-12-09 | some validation...  |
----------------------------------------------------

I intend to use in my views and controllers like it:

$model->money_en //will return $562,00

Now the questions are:

1 - How can I use this kind of fields to store values and recovery it? 2 - How can I user different validation kinds to this fields? Since I store the validations array as a serialized values on database?

Thanks.

Was it helpful?

Solution

Maybe you should look into an EAV implementation? Someone created an EAV Behavior for Yii in the YiiExt, here (I haven't used it). EAV is complicated and (often) a bit inefficient (lots of joins!), but it's the way to go if you want to have an arbitrary number and type of fields attached to a DB "row".

OTHER TIPS

Basically what you need to accomplish here is a custom ActiveRecord base class. If you look at CActiveRecord you will see that it retrieves all kinds of meta information from your database of choice. Since in your case the meta information is in a table you would need to create a custom ActiveRecord base class that handles what CActiveRecord does.

Note that with a custom meta table like the one you describe above will not be easy to do normal search and groups operations.

Wouldn't it be easier for you if you use a simple table with id and value where the value contains JSON data?

-------------------------------------------------------------------------
| ID | VALUE                                                            |
-------------------------------------------------------------------------
| 10 | {'name':'validation','money_en':'$100','dare_crt' :'2010-12-09'} |
-------------------------------------------------------------------------

and then something like:

$record = MyTable::model()->findByPk(10); // or some thing like this
$arr = CJSON::decode($record->VALUE);
$arr['money_en'] == '$200';
$receord->VALUE = CJSON::encode($arr);
$record->save();

take a look at this extension. It has implemenation of what you need for profile fields. http://www.yiiframework.com/extension/yii-user-management

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top