Yii 模型和自定义字段
我正在创建一个应用程序,用户可以在其中创建元字段来在数据库中存储数据,该字段将接收不同类型的数据,例如日期或货币值。
我创建此字段的最初想法是存储在数据库表(例如:meta_fields)中,该字段的名称、值和验证。
在应用程序上使用它之后,我打算扩展 AR 方法 __get 和 __set 以使用名称字段作为属性。
示例:
如果在我的表 meta_fields 上我有以下值:
----------------------------------------------------
| id | name | value | validation |
----------------------------------------------------
| 1 | money_en | $562,00 | some validation... |
----------------------------------------------------
| 2 | date_crt | 2010-12-09 | some validation... |
----------------------------------------------------
我打算在我的视图和控制器中使用它:
$model->money_en //will return $562,00
现在的问题是:
1 - 如何使用这种字段来存储值并恢复它? 2 - 如何对此字段使用不同的验证类型?由于我将验证数组存储为数据库中的序列化值?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您应该研究一下 EAV 实现?有人在 YiiExt 中为 Yii 创建了 EAV 行为,这里(我没用过)。 EAV 很复杂并且(通常)效率有点低(大量联接!),但如果您希望将任意数量和类型的字段附加到数据库“行”,那么它就是可行的方法。
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".
基本上,您需要在这里完成的是自定义 ActiveRecord 基类。如果您查看 CActiveRecord,您会发现它从您选择的数据库中检索各种元信息。由于在您的情况下,元信息位于表中,因此您需要创建一个自定义 ActiveRecord 基类来处理 CActiveRecord 的功能。
请注意,使用像上面描述的那样的自定义元表将不容易进行正常的搜索和分组操作。
如果您使用一个带有 id 和 value 的简单表,其中 value 包含 JSON 数据,这对您来说不是更容易吗?
然后是这样的:
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?
and then something like:
看看这个扩展。它实现了您对配置文件字段所需的功能。
http://www.yiiframework.com/extension/yii-user-management
take a look at this extension. It has implemenation of what you need for profile fields.
http://www.yiiframework.com/extension/yii-user-management