mementifiermodule. This module will transform your business objects into native ColdFusion (CFML) data structures with 🚀 speed. It will inject itself into ORM objects and/or business objects alike and give them a nice
getMemento()function to transform their properties and relationships (state) into a consumable structure or array of structures. It can even detect ORM entities and you don't even have to write the default includes manually, it will auto-detect all properties. No more building transformations by hand! No more inconsistencies! No more repeating yourself!
Memento pattern is used to restore state of an object to a previous state or to produce the state of the object.
config/Coldbox.cfcand add the following settings into the
moduleSettingsstruct under the
this.mementostructure in order for the
mementifierto inject a
getMemento()method into the target. This method will allow you to transform the entity and its relationships into native struct/array/native formats.
this.mementostruct with the following (optional) available keys:
mementifierwill call the public
gettermethod for the property to retrieve its value. If the returning value is
nullthen the value will be an
emptystring. If you are using CF ORM and the
ormAutoIncludessetting is true (by default), then this array can be auto-populated for you, no need to list all the properties.
[ "*" ]which will tell the mementifier to introspect the object for all
propertiesand use those instead for the default includes.
Also note the
ormAutoIncludessetting, which if you are using a ColdFusion ORM object, we will automatically add all properties to the default includes.
Userobject needs to have an
avatarLinkin it's memento. Then you can add a
avatarLinkto the array and create the appropriate
getAvatarLink()method. Then the
mementifierwill call your getter and add it to the resulting memento.
DefaultInclduesarray can also include nested relationships. So if a
Rolerelationship and you want to include only the
roleNameproperty, you can do
role.roleName. Every nesting is demarcated with a period (
.) and you will navigate to the relationship.
DefaultExcludesarray can also declare nested relationships. So if a
Rolerelationship and you want to exclude the
roleIDproperty, you can do
role.roleId. Every nesting is demarcated with a period (
.) and you will navigate to the relationship and define what portions of the nested relationship can be excluded out.
getMemento()call receives an include that is listed in this array, it will still not add it to the resulting memento. This is great if you are using dynamic include and exclude lists. You can also use nested relationships here as well.
keyof the structure is the name of the property and/or relationship. Please note that if you have a collection of relationships (array), the default value is an empty array by default. This mostly applies if you want complete control of the default value.
keyof the structure is the name of the property and/or relationship. The
valueis a closure that receives the item and it must return back the item mapped according to your function.
includes, excludes, mappers and defaults. This will allow you to add upon the defaults dynamically.
ignoreDefaultsflag. If you turn that flag to
truethen ONLY the passed in
includes and excludeswill be used in the memento. However, please note that the
neverIncludearray will always be used.
getMemento()method. No problem! If you create your own
getMemento()method, then the
mementifierwill inject the method as
$getMemento()so you can do your overrides:
results- An array containing all the unique identifiers from the array of objects processed
resultsMap- A struct keyed by the unique identifier containing the memento of each of those objects.
[email protected]and call the
process()method with your collection, the unique identifier key name (defaults to
id) and the other arguments that
getMemento()can use. Here is the signature of the method: