RE: [coldbox:3271] Autowire and cfproperty

Try setting the debug param for the autowire interceptor to True, and
then watch the logs. That can help narrow down if the autowiring is
actually happening.

Also, what do you get if you cfdump out the instance scope after the CFC
has been initialized?


Well, I went to edit the config and turn on Autowire debugging.
Something must have been cached because but changing the config to
turn debugging on, I started seeing the objects when dumping the
object. Here is a run-down of my play code.

In my General handler, I have the following:
  <cffunction name="testCFC" returntype="void" output="yes">
    <cfargument name="event" required="yes">
    <cfset var rc = event.getCollection()>

    <cfset var test = getModel("CarYearsDAO")>
    <cfdump var="#test#">
    <cfset event.noRender()>

In CarYearsDAO, I have:
<cfcomponent name="CarYearsDAO" extends="model.DAO" output="false"
cache="false" cacheTimeout="0">

  <cffunction name="init" access="public" output="false"
    <cfset super.init()>
    <cfreturn this>

As you can see, CarYearsDAO extends DAO, and here is that code:
<cfcomponent name="DAO" output="false" autowire="true">
  <cfproperty name="dsn" type="coldbox.datasource.MegaSquirt"
  <cfproperty name="cbController" type="coldbox" scope="variables">

  <cffunction name="init" access="public" output="false"
    <cfset this.vars = variables>

    <cfreturn this>

Here you can see the result of dumping my object in the General

In the output, I see the "VARS" key and the value contains a structure
of dsn, cbController, and a few other things based on <cfset this.vars
= variables>, but when I try and call <cfset this.vars =
variables.dsn>, I get an undefined error... this isn't making much
sense to me as the dump is showing it's there.


Try this
<cfcomponent name="DAO" output="false" autowire="true">
       <cfproperty name="dsn" type="coldbox.datasource.MegaSquirt"
       <cfproperty name="cbController" type="coldbox"

       <cffunction name="getCBController" access="public"
output="false" returntype="DAO">
               <cfreturn variables.cbController >

getModel("DAO"). getCBController()

getModel() loads objects first then inject the dependencies. So its
mean you can use dependent objects are available in your methods for
further actions.

Again I would suggest to read model integration guide, there are more
ways to autowire as well.


Well I followed your advice and I was able to get the injected objects
in my model to work in my controller. However, when I try to access
them within the model that they are being injected to, they are
undefined. And yes, I have read through the Model and Autowire guide
without any help there.

Here is something I found out today that seems very weird. Here is my
revised DAO. Notice that my injected objects are set to a scope of
"instance". However, in my getDSN() function, it returns the DSN
object from the variables scope. I am doing this because if I return
dsn from the instance scope, it's undefined.
<cfcomponent name="DAO" output="false" autowire="true">
  <cfproperty name="dsn" type="coldbox:datasource:MegaSquirt"
  <cfproperty name="cbController" type="coldbox" scope="instance">

  <cffunction name="init" access="public" output="false"
    <cfreturn this>

  <cffunction name="getDSN" access="public" output="false"
    <cfreturn variables.dsn>


And here is the code in my handler:
<cfset var test = getModel("DAO").getDSN()> // gets the DSN object
from the model
<cfdump var="#test#"> // dumps the DSN object
<cfdump var="#test.getName()#"> // dumps the name of the DSN

So, calling getDSN() from my handler works. It works as long as I
return the dsn object from "variables" and not from "instance" as I
declared in the cfproperty tag. Am I really not understanding this at
all, or could this be some sort of fluke?

Well, according to a source who has tried this on CF8 with ColdBox
2.x, it looks to be a bug with CF9 and the CB3.x beta as his code,
which is a direct copy of mine, works exactly as expected (being able
to access the injected object within the model itself). I guess I will
try to switch back to CF8 and the ColdBox final and see if it works


Ahh seems like a CF9 bug, because instance is in variables scope,
which is created by coldbox.
So its like this variables.instance = StructNew()
Then coldbox inject dependencies in instance variable.

This bug could be because of CF9 handles properties in variables scope
for getters/setters.


Well it looks like that bug between variables and instance was a CF9
thing. However, I am still not having any luck. When I inject the DSN
object to my model, I can't use it within the model. I can only use it
within the handler that does getModel("MyDAO"). Am I mis-understanding
the use of this injection because from what I understand from the
documentation, it should work. If i inject the DSN to my model, why
would I want it to only be available in the handler? All of my data
access is within the DAO (obviously).

Let me clarify the getModel() behavior.

getModel("Name of component") actually injects your dependencies
also getModel() is available in handler only.

For example in handler like this userService = getModel

userService.getSecurityObject( ...) in getSecurityObject method you
can access injected dependencies.

"For example you can do this getMyDsn() inside the getSecurityObject()

Now you are talking about within model...? still not sure what exactly
you after.

Please provide more detail, if possible then some code sample.
We try to help you.