I keep running into this problem and resorting to OrmExecuteQuery() or some other hack, but figure I must be doing this wrong so need to get it right.
If (for example), I have a model called “CalendarEvent”, which has an association “Registrations”.
`
property name=“Registrations” singularname=“Registration” fieldtype=“one-to-many” cfc=“EventRegistration” fkcolumn=“eventID” orderby=“tstamp DESC” ;
`
Here is the EventRegsitration CFC
`
component persistent=“true” table=“EventRegistration” hint=“Calendar Event Registrations” extends=“coldbox.system.orm.hibernate.ActiveEntity”{
property name=“id” fieldtype=“id” generator=“native”;
property name=“tstamp” ormtype=“timestamp” ;
property name=“pmtStatus” ormtype=“string” length=“10” ;
property name=“pmtMethod” ormtype=“string” lenth=“10”;
property name=“pmtAmount” ormtype=“float” scale=“2”
property name=“pmtRate” ormtype=“string” length=“20”;
property name=“regStatus” ormtype=“int”;
property name=“invoiceID” ormType=“integer”;
property name=“profile” fieldtype=“many-to-one” cfc=“Profile” fkcolumn=“profileID”;
property name=“event” fieldtype=“many-to-one” cfc=“CalendarEvent” fkcolumn=“eventID”;
}
`
So then I can get a calendar event with a number of registrations associated with it.
How am I best to work with an events registrations? For example, I want to calculate the following for an event:
- How many registrations with regStatus of 1
- The sum of the pmtAmount column where regStatus = 1
Do I need to get the registrations with this.getRegistrations() and then start working on them, or is there a way I can do this in a traditinal SQL Sense using Coldbox ORM? (which is what I would like to do)
The main problem is, that I can’t run any HQL on the registrations … “WHERE eventID = ??”, because event is an association, so no property eventID.
I’ve been playing with the CriteriaBuilder and figure this is where I need to be as it will let me do what I need to do, and have this so far (in my Event Model):
`
public function registrationCountByStatus(required status){
var reg = orm.newCriteria(“EventRegistration”);
return reg.eq(‘regStatus’,arguments.status).count()
}
`
Note that I injected ORM into my model, even thought it is already extending ActiveEntity, because I found that if I worked with CriteriaBuilder in my CalendarEvent Model, I got an error as it was trying to work on the CalendarEvent model.
I think I am on the right path, but seem to be hitting walls (strange errors), so figured I must be doing something wrong.
Can anyone suggest (if you can make sense of this longwinded post) if I am on the right track, or should I be looking somewhere totally different.
Thanks So much… even if you have just read this far… appreciated
Jason