One way is to use beforeValidation() callbacks in the model that needs
the user Id's. That callback should be something like this:
<cffunction name="setCreatedBy" returnType="boolean" output="false">
<cfif StructKeyExists(session, "userId")>
<cfset this.createdBy = session.userId>
<cfreturn true>
</cfif>
<cfreturn false>
</cffunction>
I've got a plugin that already handles this for createdBy, updatedBy
and deletedBy, and you should be able to extend it easily enough.
http://cfwheels.org/plugins/listing/12
Cheers,
Andy
> --
> You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
> To post to this group, send email to cfwh...@googlegroups.com.
> To unsubscribe from this group, send email to cfwheels+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.
>
>
What you want to do here is include the users in your query, so you
need to setup the model associations accordingly. Because you've got
multiple fields all referencing the same users table, you need to
setup seperate named associations, something like this:
<cfset belongsTo(name="creator", class="user", foreignKey="createdBy")>
<cfset belongsTo(name="updater", class="user", foreignKey="updatedBy")>
<cfset belongsTo(name="signee", class="user", foreignKey="signedBy")>
Then you can use the include argument on finder methods when you need
the user information.
Pierre did a good job of explaining all that. I agree that for your
example having two belongsTo() associations for your Major would make
the most sense.
<cfset belongsTo(name="AdminContact", class="contact", jointype="outer")>
<cfset belongsTo(name="ProgramContact", class="contact", jointype="outer)>
All your contacts would be stored in a seperate table, and you simply
have two foreign key columns in your Major's table.
@Pierre
I think what you are referring to is called Single Table Inheritance,
and while it's not yet natively supported by Wheels, Per had a great
blog post on this here:
http://per.djurner.net/single-table-inheritance-in-wheels and since
then I've built a plugin for it, which will be officially released
shortly, although as always I'm happy to send anyone a BETA off-list.
- Andy
So you have:
Majors
- id
- name etc
init block:
<cfset hasMany(name="MajorContacts")>
Contacts
- id
- name
- email etc
init block:
<cfset hasMany(name="MajorContacts")>
MajorContacts
- id
- majorId
- contactId
- type
init block:
<cfset belongsTo(name="Majors")>
<cfset belongsTo(name="Contact")>
- - -
With this setup, you could do stuff like this:
<cfset contact = model("Contact").findByKey(key)>
<cfset majorsForContact = contact.majorContacts(where="type='admin'",
include="Major")>
... to return all of a contact's majors for a particular type. It's
also the most expandable method, as creating a new type of contact
would be as simple as a new entry in the type column.
Andy