如何使用 Castle ActiveRecord 将表记录映射到任何类型
我知道可以创建 Any
关系,其中相关记录可以是任何类型。
有没有办法告诉 ActiveRecord 表中的记录属于许多不同的类型,即使没有关系?例如,我有一个表,其中有一个字符串字段,用于存储表中每条记录的类型。我希望 ActiveRecord 能够识别每个记录的类型,并随后在查询该表时实例化正确的类型。
谁能说这是否可能?
I know it's possible to create Any
relationships where the related record could be of any type.
Is there a way to tell the ActiveRecord the records in a table belong to many different types even when there is no relationships? For example I have a table in which there is a string field that stores the type of each record in table. I'd like ActiveRecord to recognize the type of each record and subsequently instantiate the correct type when querying that table.
Can anyone say if that's possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有三种方法可以做到这一点。每种方法的设计都适合以下三种情况:
单表继承
使用带有鉴别器列的单个表来确定每行包含哪种类型。类表继承
涉及为每个类使用不同的表,其中“基”表定义主键,而其他表“继承”它。具体表继承是映射类层次结构的第三种方法,每个具体类都有自己的数据库表。
http://www.castleproject.org/activerecord/documentation/v1rc1/用户指南/typehierarchy.html
There are three ways of doing this. Each method is design to suit each of these three situations:
Single Table Inheritance
uses a single table with a discriminator column to determine which type each row contains.Class Table Inheritance
involves using different tables for each class where the "base" table defined the primary key, and the others "inherit" it.Concrete Table Inheritance
is a third way to map a class hierarchy, each concrete class has its own database table.http://www.castleproject.org/activerecord/documentation/v1rc1/usersguide/typehierarchy.html