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:
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. Expect this to come to an Apptacular near you soon.
3 thoughts on “Some Things About MappedSuperClass in ColdFusion 9.0.1”
Pictures man… lol. Show us in pictures. 🙂 Do a chart to explain this and many more will get past the words. (especially on a post like this)
What would happen if you explicitly set the getFirstName() function in the artists.cfc, would this still happen?
Thanks a lot for this post. I just wondered about this topic, when I was looking at the generated code – now its clear. Even without pictures 😉