[coldbox:19678] [ColdBox-3.7.0] Not Preserving Form

I wouldn’t call that wrong if that’s what Tim wants. I’ve done the same myself before. Just make sure you realize the getter will be called on the object when the parameters to the method are evaluated regardless of whether our not it’s used.

That is true Brad, I thought I cleared that up later…

Hi

@Andrew we can do more enhancements, if HTMLHelper entityFields Method does not provide as you may like :slight_smile:

Sana, It is more like I don’t know anything about it and what it actually does. But from what I can see is it take an entity and provides almost a scaffolding for an entity.

I think that is over kill, for someone who wants to control a form and how it is displayed. Or am I not understanding that method you pointed out?

Hey Everyone,
I need to basically do a getModel(“myModel”).myfunction from within a model. I need to be able to access functions in different components.

for example.

from myHandler.cfc → I call MyModel.cfc using getModel. and from within MyModel.MyFunction I need to be able to “invoke/access” MyModel2.myfunction2 which is in a completely different CFC file all together. How can I do this?

can anyone help me out?

What is the relationship between Mymodel and MyModel2? Are both of them singletons, or transients, or a mix?

Generally speaking, you will use WireBox to inject in the references you need. Here’s two rules of thumb (These apply to handlers as well as models. Just remember, handler’s are singletons as long as handler caching is turned on):

  1. If the dependency has a scope (lifespan) of equal or longer than the target model (such as a singleton being injected into a transient or another singleton) you may inject a direct reference.
  2. In all other cases, inject the injector and ask it for the object every time you need it, storing the dependency only in a local scope
    If we’re in the first scenario, myModel would look like this:

component {
property name=“myModel2” inject=“myModel2”;

function myFunction() {
myModel2.doSomething();
}
}

In that sample, when WireBox creates myModel, it will also automatically create myModel2 (if necessary) and place a reference to it in myModel’s variables scope. An example of this would be if myModel2 is a service or a DAO-- an object that only exists once for your entire application and myModel can live with the same object for the duration of it’s life.

Now, in our second scenario, myModel would look like this:

component {
property name=“wirebox” inject=“wirebox”;

function myFunction() {
wirebox.getInstance(“myModel2”).doSomething();
}
}

So the difference here is that instead of hard-wiring myModel2 in, we’ve placed a reference to our object factory (WireBox) and we’ll ask it to give us myModel2 every time we need it. Any example of this would be if myModel2 is a short-lived object, such as the logged in user, and every call to myFunction needs to deal with a different copy of myModel2. Note, myModel2 never gets stored in any persistent scope of myModel (variables, this);

Also, please read our quick reference card on WireBox where I discuss things like scope-widening injection as well as other types of injection. (The examples above are called “mixin” injection.
https://github.com/ColdBox/cbox-refcards/raw/master/WireBox/WireBox-Refcard.pdf

And if you’re still wanting more, here are the comprehensive docs on dealing with objects:
http://wiki.coldbox.org/wiki/WireBox.cfm

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Brad,

Don’t forget that getModel does actually use wirebox as well, so this is obsolete.

Which means this comment is wrong.

Now, in our second scenario, myModel would look like this:

component {
property name=“wirebox” inject=“wirebox”;

function myFunction() {
wirebox.getInstance(“myModel2”).doSomething();
}
}

As it can be written like this as well.

component {

function myFunction() {
var results = getModel(“myModel2”).doSomething();
}
}

As it is the same thing, getModel with create an instance if it is required. Yes using wirebox or another DI framework will do the same thing. It just comes down to preference on whether you need it across the object or you only need it’s reference for that function.

Sorry Andrew, but that won’t work. The “getModel()” function is available in handlers, views, layouts, interceptors, etc. Basically, any “framework” object that extends frameworkSuperType. Models however, do not have access to the getModel() function unless you create it. Therefore, if you want a model to have access to ad-hoc objects, you need a reference to WireBox.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

hehe, yeah unless you also inject the control in then you can do the below, but your right on its own won’t work.

component {

property name=“controller” inject=“coldbox”;

function myFunction() {
var results = controler.getModel(“myModel2”).doSomething();
}
}

Hey Guys,
I am new to cfscripting format and am currently not using it. Is it a requirement for my models? If not, could you please show me the formatting if not in cfscript?

Thanks again!

not at all Brian.

lol… Sorry didn’t read your last question.

Are you sure, you know that the difference in syntax is almost the removal or use the is the same as ; and { } are code blocks?

So it then looks like

not hard at all!

Scenario 1:

Scenario 2:

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Guys,
Thank you soooo much for the extra help. Do I need to turn on WireBox? Using the new code of…

I get the following error:

Error Messages: variable [CONTROLLER] doesn’t exist

Is your model in the models folder?

WireBox is enabled by default. If fact, if you used getModel() to create your myModel, then that IS WireBox at work. Have you reinitted your application. CFC metadata is cached.

mysite.com/index.cfm?fwreinit=1

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Sorry for the delay (pesky conference call) :slight_smile:

Yes, the model is in the Model\ProviderData\base.cfc

Ok, yes my application works when I use getModel() from any handler so I guess that’s a good sign :slight_smile:

Just tried reinitialize and still get the same error.

I have posted the stack trace if it helps at http://toolkitgroup.com/stacktrace.cfm

again my here is my handler code

<cfset rc.qry = getmodel(“eSchedule\shiftBid”).ValidateAndRecord()>

>

<cfset event.setView(“eSchedule\BidCenter\submitBid”)>

Here is my model code

Thanks in advance!

So, to confirm, you have a file called “base.cfc” in the folder /model/providerData?

/model/providerData/base.cfc

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

That is correct.