Hey just thought I’d share some code that has made my life a bit easier when dealing with linked tables and cascading saves.  This code will return a translated error array of all errors that the object has including child objects (and their children and there children’s children…)

 

I put this in the model\data\record\[tablename]Record.cfc files (you could possibly put this in the abstractRecord.cfc instead of all your Record.cfc files if you wanted to modify they reactor core but I haven’t tested that and it makes upgrading reactor a messy affair).

 

Any comments or suggestions are welcome.

 

            <cffunction name="getAllErrors" returntype="array" access="public" output="false" >

                        <!---

                        steps:

                        1. Function setup

                        2. grab parent error object

                        3. iterate through children nodes

                        4. grab children errors

                        5. Merge Child errors with Error Object

                        6. Return Errors

                         --->

                        <cfset var ErrorCollection ="" />

                        <cfset var Errors = ArrayNew(1) />

                        <cfset var childrenArray = ArrayNew(1) />

                        <cfset var childErrors = ArrayNew(1) />

                        <cfset var i = "" />

                        <cfset var j = "" />

                        <cfset var item = "" />

                        <!--- Grab the parents Errors and store them in the Errors Object --->

                        <cfset ErrorCollection = _getErrorCollection() />

                        <cfset Errors = ErrorCollection.getTranslatedErrors() />                

 

                        <!--- validate all loaded children --->

                        <cfloop collection="#variables.children#" item="item">

                                    <!--- check to see if this child is loaded --->

                                    <cfif IsObject(variables.children[item])>

                                                <!--- Store all the records for this child in an array (hasMany relation) --->

                                                <cfset childrenArray = variables.children[item].getArray() />

                                                <!--- Loop over each record and add it's errors to the Errors array --->

                                                <cfloop from="1" to="#arrayLen(childrenArray)#" index="i">

                                                            <!--- Woot Recursion --->

                                                            <cfset childErrors = childrenArray[#i#].getAllErrors() />

                                                            <cfloop from="1" to="#arrayLen(childErrors)#" index="j">

                                                                        <cfset arrayAppend(errors, childErrors[#j#]) />

                                                            </cfloop>

                                                </cfloop>

                                    </cfif>

                        </cfloop>

                        <cfreturn Errors />

            </cffunction>

 

 

Use:

<cfset tableRecord = variables.Reactor.CreateRecord("table") />

<cfset childTable = pcardRecord.getMealIterator().Add() />

<!--- populate tableRecord and ChildRecord with data -à

<cfset tableRecord.Validate() />

<cfset errors = tableRecord.getAllErrors() />

 

Ryan Blatz

 


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Reactor for ColdFusion Mailing List
[email protected]
Archives at: http://www.mail-archive.com/reactor%40doughughes.net/
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Reply via email to