开发者

How do I convert an entity from one type to another in ColdFusion ORM?

开发者 https://www.devze.com 2023-04-10 11:17 出处:网络
Let\'s say I have three entities, Company, StandardCompany, and CompanyTemplate, where Company is the base class for the other two:

Let's say I have three entities, Company, StandardCompany, and CompanyTemplate, where Company is the base class for the other two:

                Company
                  /\
                 /  \
                /    \
  StandardCompany    CompanyTemplate

And the component mappings:

component name="Company" mappedSuperClass="true"
{
    property name="Name";
}

component name="StandardCompany" persistent="true" extends="Company"
{
    property name="Owner";
}

component name="CompanyTemplate" persistent="true" extends="Company"
{
    property name="UsageCount";
}

The user should have the ability to convert any StandardCompany to a CompanyTemplate and back again. I know I can create a new CompanyTemplate whenever the user wants to convert a StandardCompany:

function ConvertToTemplate(StandardCompany company)
{
    var template = EntityNew("CompanyTemplate");
    // copy all properties from company to template
    EntitySave(template);
}

But this will create a new record and break any bookmarks to the old company. I'd like to be able to just convert it to a CompanyTemplate without creating any new records. I'm using table-per-hier开发者_如何转开发archy mapping.

How can I accomplish this by just modifying the type of the Company, without having to create a new record for it?


How can I accomplish this by just modifying the type of the Company, without having to create a new record for it?

You cannot. You're using mappedSuperClass, meaning you're storing StandardCompany and CompanyTemplate in seperate table.

If you use Table per subclass with discriminator, you can change the value of the discriminatorColumn then you can modify the type "without having to create a new record".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号