Problems with Model and Injections

Hi Guys

I have made many advances in cold box. Only a problem which I can not solve.

I have a script, that should connect to the database. But when I try to access a dump, I get the error message “variable dbname not found”. Where there seems to be problem?

The script is as follows:
component name=“AuthenticationService “accessors=“true” extends=“coldbox.system.EventHandler” output=“false” autowire=“true”{
property name=“Usermail” type=“string” default=””;
property name=“Userpass” type=“string” default="";

property name=“dbName” inject=“coldbox:datasource:mavokPortal”;

AuthenticationService function init() {
return this;
}

public boolean function process(rc) {
// set the properties with values passed by rc
setUsermail(rc.Usermail);
setUserpass(rc.Userpass);

// do some minor validation
if(
trim(Usermail) == “” or
trim(Userpass) == “”
){
return false;
}
writeDump(dbname.getName());
}

Can anyone help me?

Kind Regards
Andy

Is this a Service or a Handler?

It is a service and I call it on a Login.cfc handler with followed code:

function process(event,rc,prc) {
Event.noRender();
Event.paramValue(“Usermail”,"");
Event.paramValue(“Userpass”,"");

if(!isDefined(“session.AuthenticationService”)) {
setNextEvent(“login.signin”);
};
if(Session.AuthenticationService.process(rc)){
setNextEvent(“login.successful”);
} else {
getPlugin(“MessageBox”).setMessage(“error”,"#getResource(‘error-title-error’)# Angaben zum Login nicht gültig.");
setNextEvent(“login.signin”);
}
}

Then you will want to change the extends on the component to match what service your extending, secondly where is the service saved, what folder?

Another thing to check, is if the service is to extend for ORM purposes, then you may wish to double check that the cfclocation is picking it up as well.

I am also assuming that this is ColdBox 3.5 as well…

Then you will want to change the extends on the component to match what service your extending, secondly where is the service saved, what folder?

Currently, it is in the model folder. Is that wrong?

If extends is wrong, then it must be extends = "coldbox.system.ioc.config.Binder so that I can access the config?

Excuse me, maybe I do not understand the context of model completely.

No the model folder is where one should be putting it.

But is this ColdBox 3.5?

And is this an ORM extended service?

And accessing the config is not the issue, wirebox should take care of all that for you.

Just remove the extends if this is not an ORM service.

I have now removed. The error seems to occur further.

component name="AuthenticationService "accessors=“true” output=“false” autowire=“true”{
Error Messages: Variable DBNAME is undefined.

But is this ColdBox 3.5?

Yes, it is version 3.5.0 set up as a ColdFusion mapping

Ok, I didn’t think that would fix it. Now the thing that I need to ask is this

property name=“dbName” inject=“coldbox:datasource:mavokPortal”;

Assumes that you have the datasource setup in the config.cfc, so you will need to double check this.

The databases have been defined correctly. The following is in the Coldbox.cfc

Ok, at what stage is the service being stored in the session?

And is there any reason you’re storing it in the session?

Here is what I think is happening, the service is being stored in the session before the configuration is being setup (just a guess at the moment), when you’re then accessing it it will be null. If you removed it from the session and then just injected it into the handler, and referenced it normally from there, do you get the same result?

Ok, at what stage is the service being stored in the session?
Sorry I do not quite understand.

The reason for this was that, that the handling with session is easier and possibly safer for me. Correct me if this is not so.

Here is what I think is happening, the service is being stored in the session before the configuration is being setup (just a guess at the moment), when you’re then accessing it it will be null. If you removed it from the session and then just injected it into the
handler, and referenced it normally from there, do you get the same result?
Your guess sounds very logical. How would you conduct the call if not in a session? Can you give me an example in this regard so that I can rebuild it?

I have now rewritten all so that all takes place in the application scope. Since the same problem:

  • Variable dbname not found.

Moving it to another scope is not going to fix the problem, you still haven’t told us how your storing it in the session (Application scope now) that is crucial for us to help you.

But I will still ask why your storing it in a session or scope like this?

Thanks for the detailed information. With this information I will be able to start something.

Thus the question about the session can still be resolved, here is the snippet of the code.

handlers/Login.cfc

`
component name=“login” extends=“coldbox.system.EventHandler” output=“false” autowire=“true” {

public any function init(controller) {
super.init(arguments.controller);
return this;
}

function index(event,rc,prc) {
setNextEvent(“login.signin”);
}

/*

  • ====================================
  • Login prozedure
  • ====================================
    */
    function signin(event,rc,prc) {
    Event.setValue(“subpageTitle”,“Login”);
    if(!isDefined(“session.AuthenticationService”)) {
    session.AuthenticationService = new model.AuthenticationService();
    }
    Event.setView(‘login/Login.Signin’);
    Event.setLayout(‘Layout.Login’);
    }

/*

  • ====================================
  • Check Login credentials prozedure
  • ====================================
    */
    function process(event,rc,prc) {
    Event.noRender();
    Event.paramValue(“Usermail”,"");
    Event.paramValue(“Userpass”,"");

if(!isDefined(“session.AuthenticationService”)) {
setNextEvent(“login.signin”);
};
if(Session.AuthenticationService.process(rc)){
setNextEvent(“login.successful”);
} else {
getPlugin(“MessageBox”).setMessage(“error”,"#getResource(‘error-title-error’)# Login information not valid");
setNextEvent(“login.signin”);
}
}

/*

  • ====================================
  • Login prozedure successful
  • ====================================
    */
    function successful(event,rc,prc) {

// Enable this debug
// structDelete(session, “AuthenticationService”);
getPlugin(“MessageBox”).setMessage(“info”,"#getResource(‘error-title-success’)# Login erfolgreich.");
setNextEvent(“Login.mymavok”);
}

`

model/AuthenticationService.cfc

`
component name=“AuthenticationService “accessors=“true” output=“false” autowire=“true” {
property name=“Usermail” type=“string” default=””;
property name=“Userpass” type=“string” default="";

property name=“dbName” inject=“coldbox:datasource:mavokPortal”;

AuthenticationService function init() {
return this;
}

public boolean function process(rc) {
// set the properties with values passed by rc
setUsermail(rc.Usermail);
setUserpass(rc.Userpass);

// do some minor validation
if(
trim(Usermail) == “” or
trim(Userpass) == “”
){
return false;
}
}
}

`

I hope this information helps

You are creating objects manually, which defeats the purpose of WireBox.

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

Also the init function is not needed in handlers, as the controller is always available for handlers.