[Coldbox 4.1] Cannot get entity injection to work.

I’m trying to get entity injection to work in a CB 4.1/CF11 app. I have the following:

application.cfc:

this.mappings["/models"] = getDirectoryFromPath(getCurrentTemplatePath()) & “models/”;
this.mappings["/cborm"] = getDirectoryFromPath(getCurrentTemplatePath()) & “modules/cborm”;

// ORM configuration
this.dataSource = “Brotherhood”;
this.ormEnabled = true;
this.ormSettings = {
cfclocation = “/models”,
dbcreate = “none”,
dialect = “MicrosoftSQLServer”,
flushatrequestend = false,
autoManageSession = false,
eventHandling = true,
eventhandler = “models.ORMEventHandler”

models/ORMEventHandler:

component extends=“cborm.models.EventHandler”{

}

partcipant.cfc:

component persistent=“true” table=“tblParticipant” entityname=“participant” schema=“dbo” output=“false”
{

property name=“arrayOfStructsSort” inject=“models:UDF:arrayOfStructsSort:{this}” scope=“this” persistent=“false”;

property name=“logger” inject=“logbox:logger:{this}” persistent=“false” scope=“this”;


}

Both logger and arrayOfStructsSort are undefined when I dump the entity. I have successfully used entity injection in my 3.8. Can’t seem to find what I am missing. Any ideas where to look next?

thanks in advance.

Byron

How are you creating the ORM entity?

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

I’m calling it through my handler:

component{

property name=“participantService” inject=“models.participantService”;

function edit(event,rc,prc){

rc.participant = participantService.get( rc.ID );

event.setView(“participants/edit”);

}

Byron

FWIW, the “models.” part should be optional since models is the default scan location. Can you show the service? Does it extend a ColdBox ORM service?

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Do you have ORM injection enabled in your config?

orm = {
injection = {
// enable entity injection
enabled = true,
// a list of entity names to include in the injections
include = “”,
// a list of entity names to exclude from injection
exclude = “”
}
};

Curt

Here is my participantService:

component extends=“ahb.models.baseService” singleton{

/**

  • Constructor
    */
    public participantService function init(){

// init super class
super.init(entityName=“participant”);

// Use Query Caching
setUseQueryCaching( false );
// Query Cache Region
setQueryCacheRegion( ‘ORMService.defaultCache’ );
// EventHandling
setEventHandling( true );

return this;
}

baseService.cfc

component extends=“cborm.models.VirtualEntityService” accessors=“true” singleton output=“false” {

public BaseService function init( entityName ) {
super.init( entityName=arguments.entityName );
return this;
}

}

Yes. It is enabled. Done many a fwreinit and ORMreload.

Byron

Note, entity injection needs to be enabled in two places. The Application.cfc, and in the Coldbox config struct that the cborm module uses. I do believe the default is true though if you don’t have the ORM struct in your ColdBox config.

https://github.com/ColdBox/cbox-cborm

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Making headway.

If I use:

eventhandler = “cborm.models.EventHandler” in this.ormSettings in application.cfc,

it works as expected.

However, I have my own models/ORMEventHandler.cfc where I extend “cborm.models.EventHandler”. When I do that, i do not get the injection. I use my own eventhandler to do things like update variables for all my tables like so:

public void function preUpdate( any entity, Struct oldData) {

var currentTime = now();

if (structKeyExists(arguments.entity,“setDateModified”))
arguments.entity.setDateModified(currentTime);
if (structKeyExists(arguments.entity,“setModifiedBy”) && len(getInstance(‘sessionStorage@cbstorages’).getVar(“user”).getID()))
arguments.entity.setModifiedBy(getInstance(‘sessionStorage@cbstorages’).getVar(“user”).getID());

}

If there is a better way to accomplish that, I’m happy to take a look.

Byron

Are you calling the super methods in your own ORM event handler?

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

No. What method(s) do I need to ‘super’ for the injection to fire? Or do I need to have an init function that does the super?

Every method you override in the base class will need to call the super method. Otherwise, you are negating the base class entirely as the base method never gets called.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Thanks for the feedback. I think I missing something. The cfc you specific in this.ormsettings.eventhandler is supposed to be a global event handler. All the methods in my models.ORMEventHandler.cfc fire correctly without having to super them. What should I be using to trigger the entity injection? Something simple is probably escaping me…

Byron

Hooray. Working. In an earlier 3.8 app I was not using postLoad() or postNew() methods. I am on this 4.1 app. Added super. on those 2 methods and bingo.

Thanks for your help.

Byron