Skip to content

Instantly share code, notes, and snippets.

@ifedotov
Created October 21, 2012 02:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifedotov/3925532 to your computer and use it in GitHub Desktop.
Save ifedotov/3925532 to your computer and use it in GitHub Desktop.
Person.cfc with ColdFusion ORM settings for many-to-many relationship on same entity
<cfscript>
component output="false" persistent="true" table="Person" {
property name="personid" column="personid" generator="identity" index="true" fieldtype="id" ormtype="int" insert="false" update="false";
property type="timestamp" name="created" generated="insert";
property name="name" ormtype="string";
property
name="guides"
singularname="guide"
fieldtype="many-to-many"
CFC="Person"
linktable="PersonToPerson"
lazy="true"
cascade="all"
orderby="name"
inverse="true";
property
name="followers"
singularname="follower"
fieldtype="many-to-many"
CFC="Person"
linktable="PersonToPerson"
lazy="true"
cascade="all"
orderby="name";
function init() {
variables.followers = [];
variables.guides = [];
}
public void function addGuide(required Person guide) {
if (not hasguide(arguments.guide)) {
// set this side
arrayAppend(variables.guides,arguments.guide);
// set the other side
arrayAppend(arguments.guide.getFollowers(),this);
}
}
public void function removeGuide(required Person guide) {
if (hasguide(arguments.guide)) {
// set this side
var index = arrayFind(variables.guides,arguments.guide);
if (index gt 0) {
arrayDeleteAt(variables.guides,index);
}
// set the other side
index = arrayFind(arguments.guide.getFollowers(),this);
if (index gt 0) {
arrayDeleteAt(arguments.guide.getFollowers(),index);
}
}
}
public void function addFollower(required Person follower) {
arguments.follower.addGuide(this);
}
public void function removeFollower(required Person follower) {
arguments.follower.removeGuide(this);
}
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment