Execute a function onSessionStart

Hi everyone,
Still trying to learn Coldbox.

I need to execute a function onSessionStart.
I have created the function in the handler for the model service and it executes correctly.
In coldbox.cfc I have set the Impicit Events

sessionStarthandler = “Main.onSessionStart”

In Main.cfc I have
property name=“periodService” inject=“model:PeriodService”;

But when I try to call the function in onSessionStart
periodService.generate();

I get an error

Error Messages: Invalid method call: generate
The dynamic/static method you called does not exist

Can some please explain what I’m doing wrong.

Does periodService have a method called generate()? That error is coming from the onMissingMethod handler in baseORMService.

https://github.com/Ortus-Solutions/ContentBox/blob/master/coldbox/system/orm/hibernate/BaseORMService.cfc#L1049

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 have created the function in the handler file
public void function generate()

I’m confused then as why you are trying to call the generate() method on the periodService as you showed in this code:

> periodService.generate();

This has nothing to do with ColdBox and is just how ColdFusion works.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Because the generate function is part of the periodService.
Then how would you call it?

Wait, you said you “created the function in the handler file”. Which is it? Is the “generate” method in the periodservice, or the handler (/handlers/main.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

This is what I have

Model :
PersiodService (init)
Period properties as per db

Handlers:
Periods (generate)
Main (onSessionStart) (this is where I want to call the function from as I only want it to execute when a new session starts

So you have an event called “periods.generate”. Just call
runEvent( ‘periods.generate’ );

Now, I assume you might want to inject periodservice into your “periods” handler as you might be making a service call there. As your code stands now, you’re trying to call a method on your service that actually exists in a 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

I learn best through examples, so the examples I have found all have the index, list, edit, save and remove functions in the “handler” file.
As the generate function really only inserts some new rows (dependant upon matching criteria) I thought this would be the logical place.

Is this incorrect?
If so, where can I find some correct examples?

Honestly, I have no idea what you’re trying to do so I can’t really tell you if you’re doing it right :slight_smile: Chances are, you’re assuming there’s conventions or best practices where there really are none. You can do whatever you want with your handlers, and what methods (actions) you create inside them. A lot of our examples in our docs show simple CRUD operations, so stuff like edit, save, list, etc makes sense. You should create whatever actions you need based on what makes sense for your website. Start by asking what screens you expect the user to interact with. Each of those should PROBABLY be an action in your handler. Remember, forms will need a place to submit, so don’t forget to have processing actions.

Now, as far as your onSessionStart, that is as wide open as the Kansas plains. If you want to re-use an existing action you already have created, then run it with runEvent(). If you want to call a service method directly, inject that service and call its method. If it were me, I wouldn’t create an action (method in your handler) unless you plan on hitting it directly from some client (browser, REST API consumer, etc). When I first started using ColdBox I wanted to put everything in a handler, but eventually I realized i was just wasting my time and putting business logic where it didn’t belong.

My advice is to think about your service later as the API for what your site does and create a method in your services for every operation. Now, call these service method as necessary from the appropriate place in your application. If you want to do something at the beginning of each request, ask your service to do it from a prePrecess() interceptor. If you want to handle a remote Flex client, ask the service to do it from a remote proxy. And if you want to display HTML or process a form on behalf of a user’s web browser, ask your service to do it from a handler’s action.

If it works for you and creates good, manageable code, there’s no wrong way. But at the end of the day, you can’t call periodService.generate() if there’s no generate() method in your periodService :slight_smile:

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 you time and insight Brad.
I have it working with runEvent and will now look into moving the function to the service.
This is the learning curve of OO and using a framework for the first time.

Ken