Created
October 1, 2014 15:51
-
-
Save ifedotov/60f369eba4b8b3987a95 to your computer and use it in GitHub Desktop.
Coldfusion ORM 2 one-to-many mappings in place of 1 many-to-many
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component | |
{ | |
this.name = "many_to_many"; | |
this.datasource = "test_mysql"; | |
this.ormenabled = true; | |
this.ormsettings.dbcreate = "update"; | |
this.ormsettings.flushAtRequestEnd = false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
main(form, url); | |
public any function main(form, url) { | |
args = structAppend(form, url); | |
var permissions = create_permissions(); | |
var people = create_people(); | |
people[1].addPermission(permissions[1]); | |
people[1].addPermission(permissions[2]); | |
writeDump(var=people[1].hasPermission(permissions[1])); | |
writeDump(var=people[1].hasPermission(permissions[3])); | |
writeDump(var=people); | |
} | |
public any function create_permissions(args) { | |
transaction { | |
var permissions = entityLoad("Permission"); | |
if(arrayIsEmpty(permissions)) { | |
var permissions = [ | |
new Permission(permission = "add"), | |
new Permission(permission = "edit"), | |
new Permission(permission = "delete") | |
]; | |
for(var p in permissions) { | |
entitySave(p); | |
} | |
} | |
} | |
writeDump(var=permissions); | |
return permissions; | |
} | |
public any function create_people(args) { | |
transaction { | |
var people = entityLoad("Person"); | |
if(arrayIsEmpty(people)) { | |
var people = [ | |
new Person(firstname = "John", lastname="Doe"), | |
new Person(firstname = "Mary", lastname="Smith") | |
]; | |
for(var p in people) { | |
entitySave(p); | |
} | |
} | |
} | |
return people; | |
} | |
</cfscript> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component persistent="true" table="permissions" { | |
property name="id" fieldtype="id" setter="false" column="permission_pk" generator="native"; | |
property name="permission" length="50"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component persistent="true" table="people" { | |
property name="id" fieldtype="id" setter="false" column="person_pk" generator="native"; | |
property name="firstname" length="50"; | |
property name="lastname" length="50"; | |
property name="PersonPermissions" fieldtype="one-to-many" cfc="PersonPermissions" fkcolumn="fk_personid" type="array" singularname="PersonPermission"; | |
public any function init() { | |
setPersonPermissions([]); | |
return this; | |
} | |
public any function addPermission(permission) { | |
var person_permission = new PersonPermissions(permission=permission); | |
this.addPersonPermission(person_permission); | |
} | |
public any function hasPermission(permission) { | |
for(var p in this.getPersonPermissions()) { | |
if(p.hasPermission(permission)) { | |
p.set_last_checked_at(now()); | |
entitySave(p); | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component persistent="true" table="people_permissions" | |
{ | |
property name="id" fieldtype="id" setter="false" column="person_permission_pk" generator="native"; | |
property name="_last_checked_at" ormtype="timestamp"; | |
property name="_created_at" ormtype="timestamp"; | |
property name="Permission" fieldtype="many-to-one" cfc="Permission" fkcolumn="fk_permissionid"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment