[coldbox-3.5.0-Final] ActiveEntity new(properties=rc) Invalid Argument Type

Hi,

Using the new ActiveEntity service like this…

local.objCourseType = entityNew(“CourseType”).new(properties=rc);

…causes this error…

Error Messages: invalid call of the function populate (C:\website\icdms\webroot\coldbox\system\orm\hibernate\ActiveEntity.cfc), second Argument (SCOPE) is of invalid type, can’t cast Object type [Struct] to a value of type [string]

Following the code from ActiveEntity.cfc to VirtualEntityService.cfc to BaseORMService.cfc and then back to the populate() method in ActiveEntity.cfc it seems that this method doesn’t take the “target” argument supplied by BaseORMService.populate() therefore I would suggest in ActiveEntity.cfc changing…

any function populate(required struct memento,
string scope="",
boolean trustedSetter=false,
string include="",
string exclude=""){
arguments.target = this;
return beanPopulator.populateFromStruct(argumentCollection=arguments);
}

…to…

any function populate(required any target,
required struct memento,
string scope="",
boolean trustedSetter=false,
string include="",
string exclude=""){
return beanPopulator.populateFromStruct(argumentCollection=arguments);

}

…or remove it completely and let inheritance find the populate() in BaseORMService.cfc

The same would apply to populateFromJSON(), populateFromXML(), populateFromQuery() I would guess but I’ve not looked into it.

Thoughts?

Ciao for now,

Richard

Here’s some more info…

Engine: Railo Express 3.3.2.003 final

Entity: CourseType.cfc

component entityname=“CourseType” table=“COURSETYPE” extends=“coldbox.system.orm.hibernate.ActiveEntity” persistent=“true” {

property name=“id” column=“ID” fieldtype=“id” generator=“assigned” length=“35” setter=“false”;

property name=“code” column=“CODE” ormtype=“string” notnull=“true”;
property name=“name” column=“NAME” ormtype=“string” notnull=“true”;

public void function init() {
super.init(useQueryCaching=true);

variables.id = createUUID();

}
}

From handler: courseType.cfc

local.objCourseType = entityNew(“CourseType”).new(properties=rc);

I took this approach after reading the doc’s…

http://wiki.coldbox.org/wiki/ORM:ActiveEntity.cfm

…which implies that this is the right way to use ActiveEntity.

Have you tried

local.objCourseType = entityNew(“CourseType”).new( {properties=rc} );

as per the docs?

I have tried that and it didn’t help also I don’t think it’s right.

Where does give an example like that with the properties argument nested inside a structure?

The properties argument to new() is expecting a struct which rc is.

The doc’s say “You can also pass in a structure called properties that will be used to populate the new entity with…”

If you walk through the chain of components you’ll find ActiveEntity passes the new() method onto VirtualEntityService.cfc which adds the entityName and then passes it onto BaseORMService.cfc where it expects entityName and optionally properties.

The new() method in BaseORMService uses the populate method which it finds in the originating ActiveEntity.cfc but this method expects the first argument to be memento rather than the entity (target) sent from new() in BaseORMService.cfc

Therefore, as I said above, either the populate() method in ActiveEntity needs changing to expect entity (target) as the first argument or deleting so that populate() is drawn down from BaseORMService.

Or have I got the chain of events totally wrong?

Richard

Yeah the chain is correct, but it has confused me as well, especially when you look at the new method as you have done and it expects something different.

Anyway what you are doing is correct, except that I have never done it the way you are doing it. So this maybe a bug that you have discovered.

As you don’t say where you are using this, I will assume either your own service or a handler, either way try doing it this way instead and see how you go.

Inside a handler

inject the service like

property name=“activeEntity” inject=“coldbox:activeEntity”;

then you could then use it like this

activeEntity.new(properties=props);

I personally wouldn’t use the handler to do this, and would be more inclined to create a service that extended the activeEntity or VirtualEntityService. which you could then do something similar.

Do you have an example that you could put together, so that we can see how you are using it, to give us an idea on how it is not working and ultimately provide a solution or fix. In other words, if this is a handler create a very basic handler that would demonstrate this problem, if it is a service could you do the same. I would like to take that and run with it and either give you back a working solution, or if it is indeed a bug, and maybe track it down.

And with the courseType, give us a very basic version of this that shows us how you have this setup, it could be that the problem lies in how you have created this entity.

Richard, your init() method does not return this, thus your chaining fails!

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano

Humm, okay but changing my entity to…

component entityname=“CourseType” table=“COURSETYPE” extends=“coldbox.system.orm.hibernate.ActiveEntity” persistent=“true” {

property name=“id” column=“ID” fieldtype=“id” generator=“assigned” length=“35” setter=“false”;

property name=“code” column=“CODE” ormtype=“string” notnull=“true”;
property name=“name” column=“NAME” ormtype=“string” notnull=“true”;

public CourseType function init() {
super.init(useQueryCaching=true);

variables.id = createUUID();

return this;
}
}

…I still get…

Error Messages: invalid call of the function populate (C:\website\icdms\webroot\coldbox\system\orm\hibernate\ActiveEntity.cfc), second Argument (SCOPE) is of invalid type, can’t cast Object type [Struct] to a value of type [string]

I’m going to take Andrew’s advice and knock up a standalone sample later today and post it here.

Richard

Yes, please post full example so I can replicate

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano

Okay, sorry for the delay in coming back to you guys, had a couple of days off.

Attached is the simplest handler, entity and view which produces the same error as reported before…

Error Messages: invalid call of the function populate (C:\website\icdms\webroot\coldbox\system\orm\hibernate\ActiveEntity.cfc), second Argument (SCOPE) is of invalid type, can’t cast Object type [Struct] to a value of type [string]

If you then comment out the populate() method in ActiveEntity.cfc and reinit the app all seems fine.

Where could I be doing wrong?

Richard

myHandler.cfc (200 Bytes)

myEntity.cfc (289 Bytes)

myView.cfm (172 Bytes)

Richard,

I was finally able to reproduce. It is now fixed and targeted for 3.5.1 release which includes tons of fixes already. Thanks guys!

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano

Hi Luis, I’ve taken ActiveEntity.cfc and BaseORMService.cfc from the current development branch of GitHub and all seems good!

Thanks for your prompt attention :slight_smile:

Richard

Sorry Luis, I think I’ll have to retract my approval for this fix.

The order of the arguments in ActiveEntity.cfc for the populate*() methods should match those of BaseORMService.cfc or drop those methods completely from ActiveEntity.cfc and let ActiveEntity inherit them from BaseORMService via VirtualEntityService.

Richard

why is that?

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano

Because I’m still getting…

Error Messages: invalid call of the function populate (C:\website\icdms\webroot\coldbox\system\orm\hibernate\ActiveEntity.cfc), second Argument (SCOPE) is of invalid type, can’t cast Object type [Struct] to a value of type [string]

If you comment out the populate*() methods in ActiveEntity.cfc or reorder them to match BaseORMService.cfc all is good.

Well, the Base ORM services now used named parameters, so that should not be the case anymore. Are you sure, you restarted and cleaned the memory

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano

Yes, all restarted and memory cleaned.

The issue is the order of the args in populate*() in ActiveEntity.cfc don’t match BaseORMService.cfc or the API doc’s, so calling populate*() with positional args like this…

entityNew(“CourseType”).populate(local.objCourseType, rc);

…is failing because ActiveEntity.cfc expects “memento” to be first where as the doc’s and BaseORMService.cfc expects “target” to be first.

Sure I could specify the arg name in my call like…

entityNew(“CourseType”).populate(target=local.objCourseType, memento=rc);

…but I still contend that ActiveEntity.cfc should match the args order of BaseORMService.cfc and the doc’s :slight_smile:

I’ve not looked at the docs, but assuming what Richard says is accurate (and I have no reason to think otherwise), I am in agreement with him.

/0.02

:slight_smile:

Sorry, I guess I should have provided a link to the doc’s I’m referring to…

http://www.coldbox.org/documents/api/ColdBoxDocs-3.5.0//coldbox/system/orm/hibernate/BaseORMService.html#populate()

Ahh ok, I get it now

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano

Great, I see you’ve updated the development branch on GitHub to fix the order of declaration.

Thanks once again Luis for your prompt attention.

Most appreciated :slight_smile:

Richard