I'm a Worldwide Developer Evangelist for Adobe. My job basically entails me traveling the world and talking about the developer tools and technologies that Adobe has to offer or that we support. I'm also the author of Driving Technical Change. It's about convinving reluctant co-workers to adopt new tools and techniques. Terrence is a DZone MVB and is not an employee of DZone and has posted 40 posts at DZone. You can read more from them at their website. View Full User Profile

Some Things About MappedSuperClass in ColdFusion 9.0.1

04.09.2011
| 5392 views |
  • submit to reddit

I was fooling around with inheritance in super classes in ColdFusion 9.0.1. I was trying to add that whole dynamic/static inheritance thing to Apptacular so you can add on to an Apptacular application without having to worry about changes being overridden by the regeneration process. Typically when you do that, the dynamic, rewritten class is usually the parent of the child static class. The child, static, class is what you refer to in the rest of code and in which you write custom code. The scaffolder never touches this file again, allowing you to alter things in the database, rescaffold and not overwrite code customizations. I hearby call this "non-destructive scaffold regeneration," regardless of what other people call it.

ORM entities can't really work that way. If you try to setup that sort of thing, ORM craps out in ColdFusion with an error. CF9 ORM doesn't really like ORM objects being the parent of other items, unless you are using inheritance mapping.

However, if you use the "mappedSuperClass" cfc attribute that is now available in ColdFusion 9.0.1 you can achieve sort of the same thing, but you have to flip this on its head. The superclass is the one that is static, and you can edit, while the dynamic one is the child.

This is mostly good. Except for one thing: one of the main reasons to write code this way is so you can override methods, and if the child is the one that is dynamic, and overwritten, then you cannot override the methods from the static parent. You can add methods and properties, but you cannot override them.

At first glance this seems like a deal breaker, but it doesn't have to be. Most generated ORM entities are light on code, because of implicit getters and setters. But those implicit getters and setters are exactly what you want to override in an ORM entity. However it turns out that explicit functions will always override implicit ones, even when the explicit function is in the super class and not the child class.

I put together a simple little demo to show this off:

component mappedSuperClass="true"{
	public string function getFirstName(){
		return "Yo";
	}
	
	public string function getRealFirstName(){
		return variables.firstName;
	}
}
component{
	This.name = "cfartgallery";
	This.ormenabled = true;
	This.datasource = "cfartgallery";
}
component extends="_artistsSuperEntity" persistent="true"{

	property name="ARTISTID" fieldtype="id" ;
	property name="FIRSTNAME" type="string";
	property name="LASTNAME" type="string";
	property name="ADDRESS" type="string";
	property name="CITY" type="string";
	property name="STATE" type="string";
	property name="POSTALCODE" type="string";
	property name="EMAIL" type="string";
	property name="PHONE" type="string";
	property name="FAX" type="string";
	property name="THEPASSWORD" type="string";	
}
<cfset artist = entityLoad("artists", 1, true) />
<cfoutput>
	<p>GetFirstName = #artist.getFirstName()#</p>
	<p>GetRealFirstName = #artist.getRealFirstName()#</p>
</cfoutput>

Result from test.cfm:

GetFirstName = Yo

GetRealFirstName = Aiden

So it's a little upside down, but if you want to do some non-destructive scaffold regeneration with ColdFusion 9 ORM, it's a workable solution. E

References
Published at DZone with permission of Terrence Ryan, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Aiguo Bolin replied on Fri, 2011/08/12 - 5:46am

Hello

The following article lists some simple, informative tips that will help you have a better experience with .

 

I trust that what you've read so far has been informative. pass4sure E20-591 The following section should go a long way toward clearing up any uncertainty that may remain.

I was fooling around with inheritance in super classes in ColdFusion 9.0.1. I was trying to add that whole dynamic/static inheritance thing to Apptacular so you can add on to an Apptacular application without having to worry about changes being overridden by the regeneration process. pass4sure 352-001 Typically when you do that, the dynamic, rewritten class is usually the parent of the child static class. pass4sure 70-511 The child, static, class is what you refer to in the rest of code and in which you write custom code.  

Now you can understand why there's a growing interest in when people start looking for more information about, pass4sure 642-975 you'll be in a position to meet their needs.

Shoaib Almas replied on Sat, 2012/08/25 - 5:55am

What would happen if you explicitly set the getFirstName() function in the artists.cfc, would this still happen?

Java Forum

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.