So I have set my application up to point to a Custom ORM event handler…
My application is set up correctly… Everything is exactly as it is in the documentation. I’ve gone over it a million times.
In my .cfc all I have is the following
component extends="coldbox.system.orm.hibernate.EventHandler"{ }
I know coldbox.system.orm.hibernate.EventHandler has a postNew() function.
I would like to override it.
I am using ActiveEntities.
These are my assumptions...
1. The .new() function in my ActiveEntity is supposed to call the .postNew() function.
2. The .new() function which is inherited by my ActiveEntity does call the .postNew() function (Not really an assumption)
--- But...
The .postNew() function that is called within the .new() function is not the called on the EventHandler I am using for my application…
It is called on a separate instance of the EventHandler.cfc for each ActiveEntity.
Which means that my ActiveEntities are not connected in any way to t``he EventHandler used by my application.
Which means my application’s EventHandler .postNew() function will never run, cause there is NO Coldfusion 10 postNew() ORM Event…
So my question is… How do I overwrite the .postNew() function that is used by my ActiveEntity.
Things I have tried…
I assumed I could just create my .postNew() function with my ActiveEntity and it would override the default one.
Result: Didn’t work…
I changed the actual code of BaseORMService .new() function to actually call the .postNew() function in my ActiveEntity…
Result: Worked to a point… I would still have to call the .postNew() the EventHandler used by my ActiveEntity… Couldn’t figure how to reference it. ( super.postNew() does not work… nor should it…)
End Result: Didn’t work…
Overrode the .postNew() in my Application’s EventHandler…
Result: Didn’t work…
Created an Interceptor to catch the ORMPostNew event.
Result: worked! But now I have my event override functions in two separate places… .preInsert() within my ActiveEntity, which works fine. and the ORMPostNew() function in a completely different location. AND… I have write in extra code into that function to figure which ActiveEntity just go new’ed up!
End Result: works…In my opinion… extremely inelegant…
Put all Orm Event handling overrides into Interceptor…
Result: worked. But yet again… inelegant… More code… same results…
Is there a way to override the .postNew() ORM event without using an Interceptor?
Am I being to picky?
Have I done something that should have worked?
Do I have something misconfigured?
Have I completely misunderstood the whole concept and need to have it explained?
What am I missing?
Thanks for the help in advance!
Dominique
Yeah, I totally erased it… Sorry… Nice response time… I realized I had not completely finished the Subject, copied the post text, deleted the post, created a new post, pasted the old post text, added a better subject line and reposted. All why you were responding… craziness…
Yes… Though I will double check that… Though ORMReload, works just fine.
Which should indicate that the path I provided (which points to a file within my models folder) points to a .cfc that implements the IEventListener.
In the BaseORMService… in the init() function… it creates a variable ORMEventService??? that “news up” an instance of one of the EventHandler cfc’s provided by the ColdBox core. How does this newly “newed up” ORMEventService relate to the ORMEventHandler that is specified in the Application.cfc?
So I assume if you configure ORM in your Application.cfc to us a custom event handler, then all Coldfusion ORM events will call those functions in that particular EventHandler…
But… if you inject an ActiveEntity into a .cfc, it runs the init function function of itself and all its parents, of which, the base parent is BaseORMService.cfc.
When BaseORMService’s .init() function is called it creates a new instance of the EventHandler…
So when you call an ActiveEntity .new() function it technically calls the .new() function it inherits from BaseORMService.cfc
The .new() function in BaseORMService calls the .postNew() function of the ORMEventHandler it new’ed up in the .init() function.
It does not call the .postNew() function of the EventHandler that is specified in the Application.cfc
I see what you’re saying, and I’ve done some testing and get the same results you’re experiencing. The docs seem to indicate that postNew() can be overriden in the custom EventHandler, but I wasn’t able to get it to work (nor could I find an example). Overriding postLoad() works, but then again as you mentioned this is expected since it’s a Hibernate event.
So as Andrew suggested, the interception points are an option (which you’ve already tried). However, I think it should be pretty easy within that single interceptor to handle the ORMPostNew() for all your entities. One option is that you could simply check the passed entity in interceptData for whether or not it has a postNew() method, and if it does, simply call that method on the entity itself. Yes, it’s a few lines of code, but it would still allow you to have the postNew() methods on your entities themselves, rather than having to bake all the logic for each entity into your interceptor. In other words, it’s kind of like your original #4, except you don’t have to care about which entity it is, only that it follows a convention that you decide upon.
Post new event is something we did in ColdBox. It is not a Coldfusion thing as it does not exist. It will only fire when calling the new() method in any of the virtual, base or active entity services.
So an entitynew() will not trigger it. But a entityNew(“activeentity”).new() will.
Also, the .new() does call the .postNew() function, but not the one in the custom event handler that is set up in the application.cfc, which is where I assumed was where I was to override the .postNew() function…
There are already many tickets for FULL ORM Event handling, not sure what the number is at the moment and was raised back in 2008 from memory, for things like preNew() postNew() preFlush() and postFlush() to name a few.