Reputation: 1435
I'm working on a parent/sub application structure in CFML (Railo) and I'm struggling with extending my persistent (ORM) CFCs.
What I'd like is for my persistent CFCs to live in the parent application. They would contain the various properties, and a few functions dealing with core functionality.
In my sub app, I would like to extend the ORM CFCs declaring any sub app specific properties, and I would like to be able to add new functions specific to the needs of the sub app, as well as overriding any of the core functions if required, without touching the code in the parent app's CFCs.
The sub app uses its own datasource, so I'd expect to see the ORM tables generated in the sub's database on application start. If I start the parent app (which runs standalone and has its own datasource) I can see the tables generated there without issue. If I start the child app however, the tables are not generated (in either database).
I've tried adding mappedSuperclass='true'
to the parent CFCs and creating CFCs in the child app that extend the parent CFCs.
I've also tried adding the parent app's ORM folder to the array of CFCLocation folders in the ORM settings.
The only thing I'm able to use as an indication that the ORM is working, is to see whether the tables are generated in the database. If there's another way I can see if the ORM CFCs are working, I'd love to hear about it!
Here's some code to look at:
Parent image.cfc
<cfcomponent persistent="true" entityname="Image" table="tblImages_Base" extends="com.orm.SimpleBasePersistentObject" mappedSuperClass="true">
<!--- Identifier --->
<cfproperty name="sImageUUID" fieldtype="id" generator="assigned" setter="false" />
<!--- Properties --->
<cfproperty name="dtDateCreated" ormtype="timestamp" setter="false" />
<cfproperty name="dtLastUpdated" ormtype="timestamp" setter="false" />
<cfproperty name="sFileName" ormtype="string" />
<cfproperty name="iFileSize" ormtype="int" default="0" dbdefault="0" />
<cfproperty name="iWidth" ormtype="int" default="0" dbdefault="0" />
<cfproperty name="iHeight" ormtype="int" default="0" dbdefault="0" />
<cfproperty name="sImageFolder" ormtype="string" dbdefault="" />
<cfproperty name="Active" ormtype="boolean" default="0" dbdefault="0" notnull="true" />
<!--- Non persistant properties --->
<cfproperty name="sImagePath" type="string" persistent="false" />
<cfproperty name="sDefaultImageLocation" persistent="false" />
<!--- Many Images can have one image type --->
<cfproperty name="ImageType"
fieldtype="many-to-one"
cfc="ImageType"
fkcolumn="fk_sImageType"
fetch="join"
/>
</cfproperty>
</cfcomponent>
Sub image.cfc
<cfcomponent persistent="true" entityname="Image" table="tblImages_Base" extends="core.orm.Image">
</cfcomponent>
Upvotes: 2
Views: 617
Reputation: 2363
You can certainly do inheritance (single or multiple) using mappedSuperClass="true"
, but the parent CFC(s) cannot be persistent, i.e. mappedSuperClass="true"
and persistent="true"
are mutually exclusive.
I'd suggest you set up a "higher" level model which defines the base entities you want to be able to extend. Add mappedSuperClass="true"
but then use persistent="false"
and do not specifiy a table.
In your parent and sub apps you would then create your persistent CFCs which extend these super classes. I know it's not quite what you're after as the sub app won't be inheriting from the parent app, but it does allow them to share common properties/methods.
Note that you shouldn't define the identifier property in the super class: that needs to be done explicitly in each of the persistent CFCs.
The location of the "super" cfcs doesn't really matter - as long as they're accessible to your parent and sub apps. There's no need to add that location to your ORM settings. Let's say you store them in a folder called "library" mapped by "library":
/library/Image.cfc
<cfcomponent entityname="Image" persistent="false" mappedSuperClass="true" hint="I define common properties/methods and can be extended by other ORM components">
<!--- Common Properties--->
<cfproperty name="dtDateCreated" ormtype="timestamp" setter="false" />
<cfproperty name="dtLastUpdated" ormtype="timestamp" setter="false" />
<cfproperty name="sFileName" ormtype="string" />
...etc
</cfcomponent>
/apps/parent/Image.cfc
<cfcomponent entityname="Image" extends="library.Image" persistent="true" table="tblImages_Base">
<!--- Identifier --->
<cfproperty name="sImageUUID" fieldtype="id" generator="assigned" setter="false" />
...
</cfcomponent>
/apps/child/Image.cfc
<cfcomponent entityname="Image" extends="library.Image" persistent="true" table="tblImages_Base">
<!--- Identifier --->
<cfproperty name="sImageUUID" fieldtype="id" generator="assigned" setter="false" />
...
</cfcomponent>
Upvotes: 0