Skip to content

Instantly share code, notes, and snippets.

@chrisdpeters
Last active August 25, 2015 21:57
Show Gist options
  • Save chrisdpeters/4acd3ae3a6e0208e4ee5 to your computer and use it in GitHub Desktop.
Save chrisdpeters/4acd3ae3a6e0208e4ee5 to your computer and use it in GitHub Desktop.
CFWheels nestedProperties sortProperty fix
<cfcomponent extends="Model" output="false">
<cffunction name="init" output="false">
<cfscript>
hasMany(name="attemptedSolutions", shortcut="solution", dependent="delete");
nestedProperties(association="attemptedSolutions", allowDelete=true, sortProperty="sortOrder");
beforeValidation("$adjustAttemptedSolutionSortOrders");
</cfscript>
</cffunction>
<cffunction name="$adjustAttemptedSolutionSortOrders" access="private" hint="Adjusts array of attempted solutions before save happens so there aren't any null entries." output="false">
<cfscript>
if (StructKeyExists(this, "attemptedSolutions") && ArrayLen(this.attemptedSolutions)) {
local.attemptedSolutions = [];
local.arrayPos = 1;
for (local.i = 1; local.i <= ArrayLen(this.attemptedSolutions); local.i++) {
// Exception will be thrown when array element is "undefined" or null
try {
local.attemptedSolution = this.attemptedSolutions[local.i];
local.attemptedSolution.sortOrder = local.arrayPos;
ArrayAppend(local.attemptedSolutions, local.attemptedSolution);
local.arrayPos++;
}
catch (any e) {}
}
this.attemptedSolutions = local.attemptedSolutions;
}
</cfscript>
</cffunction>
</cfcomponent>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment