Not built in currently. For a many to one relationshiop like that I
typically add a custom method like
<cffunction name="getTemplate" access="public">
<cfreturn getDatsource().getActiveRecord(
className="my.template.class",
objectid=getValue("templateid")) />
</cffunction>
> I would expect this to be lazy loaded - so it would only get the
> template data from the db the first time siterecord.getTemplate() is
> called.
>
> Is there any built in functionality that helps me do this?
I load active record objects primarily as transients rather than caching
them. There's an exception in the Member/Security plugin for the onTap
framework that the members for example are cached in memory and lazy
loaded like this. What I have there is a "MemberFactory" which I get
from my IoC factory and then call getMember(memberid) and it does the
work of lazy-loading the individual member objects.
So what you might be looking for here is to override the init() method
of the active record for the site so that you can pass in a
"TemplateFactory" and then you can use that to lazy-load your template
records via a custom function that looks like this:
<cffunction name="getTemplate" access="public">
<cfset var tf = getTemplateFactory() />
<cfreturn tf.getTemplate(getValue("templateid")) />
</cffunction>
Or alternatively, dependent upon how much data and behavior is in the
template table, you might prefer to simply add the template data
directly into the site record object, so that the site gets its data
from multiple tables. This is something that we see often in content
management systems where all content has a record in a "content" table,
but then there's an extra table to hold data specific to its type (i.e.
page, news, etc.). As long as you've got a foreign key constraint
between the site table and the template table, this should be pretty
easy, you can just use this:
<cfcomponent extends="datafaucet.system.activerecord">
... properties here ...
<cfset setTable("site") />
<cfset addTable("template") />
</cfcomponent>
And then your site will have all the columns in the template table as
additional properties.
If you prefer not to have a foreign key constraint (or you have a
specific reason not to), then there are some extra arguments ot the
addTable method. They are probably "column" and "references" although
I"m not certain of that. ;)
Let me know how these hints work for you. :)
hth,
ike
--
[ ike ] founder - DataFaucet ORM
phone: 781.769.0723
Oh okay, just lazy-loading it within the individual request, not for the
lifespan of the application... yeah, that's actually a use case I've had
before, so I'm not sure why it didn't occur to me. ;)
> Adding the template data to the site record is not reallly an option,
> as the site record contains the same fields as the template - I know I
> could get round that with prefixes etc. - but I don't think this is
> appropriate in this case.
Gotcha. Yeah, and if this is a pre-existing schema (which it sounds like)
possibly with existing CF code already devoted to that structure it
probably makes more sense to keep what you have unless you suddenly
discover some really compelling reason to change. And I don't think I
would consider the addTable() technique compelling in that sense because
it doesn't add value for your end-user. ;)