Skip to content

Instantly share code, notes, and snippets.

@rip747
Created May 19, 2011 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rip747/980041 to your computer and use it in GitHub Desktop.
Save rip747/980041 to your computer and use it in GitHub Desktop.
wheels tableless model
diff --git a/wheels/model/initialization.cfm b/wheels/model/initialization.cfm
index 3755c2c..24a6c94 100644
--- a/wheels/model/initialization.cfm
+++ b/wheels/model/initialization.cfm
@@ -45,140 +45,144 @@
// run developer's init method if it exists
if (StructKeyExists(variables, "init"))
init();
-
- // make sure that the tablename has the respected prefix
- table(getTableNamePrefix() & tableName());
-
- // load the database adapter
- variables.wheels.class.adapter = $createObjectFromRoot(path="#application.wheels.wheelsComponentPath#", fileName="Connection", method="init", datasource="#variables.wheels.class.connection.datasource#", username="#variables.wheels.class.connection.username#", password="#variables.wheels.class.connection.password#");
-
- // get columns for the table
- loc.columns = variables.wheels.class.adapter.$getColumns(tableName());
-
+
variables.wheels.class.propertyList = "";
- variables.wheels.class.columnList = "";
- loc.processedColumns = "";
- loc.iEnd = loc.columns.recordCount;
- for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
+ // add calculated properties
+ variables.wheels.class.calculatedPropertyList = "";
+
+ if(len(tableName()))
{
- // set up properties and column mapping
- if (!ListFind(loc.processedColumns, loc.columns["column_name"][loc.i]))
+ // make sure that the tablename has the respected prefix
+ table(getTableNamePrefix() & tableName());
+
+ // load the database adapter
+ variables.wheels.class.adapter = $createObjectFromRoot(path="#application.wheels.wheelsComponentPath#", fileName="Connection", method="init", datasource="#variables.wheels.class.connection.datasource#", username="#variables.wheels.class.connection.username#", password="#variables.wheels.class.connection.password#");
+
+ // get columns for the table
+ loc.columns = variables.wheels.class.adapter.$getColumns(tableName());
+
+ variables.wheels.class.columnList = "";
+ loc.processedColumns = "";
+ loc.iEnd = loc.columns.recordCount;
+ for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
{
- loc.property = loc.columns["column_name"][loc.i]; // default the column to map to a property with the same name
- for (loc.key in variables.wheels.class.mapping)
+ // set up properties and column mapping
+ if (!ListFind(loc.processedColumns, loc.columns["column_name"][loc.i]))
{
- if (StructKeyExists(variables.wheels.class.mapping[loc.key], "type") and variables.wheels.class.mapping[loc.key].type == "column" && variables.wheels.class.mapping[loc.key].value == loc.property)
+ loc.property = loc.columns["column_name"][loc.i]; // default the column to map to a property with the same name
+ for (loc.key in variables.wheels.class.mapping)
{
- // developer has chosen to map this column to a property with a different name so set that here
- loc.property = loc.key;
- break;
+ if (StructKeyExists(variables.wheels.class.mapping[loc.key], "type") and variables.wheels.class.mapping[loc.key].type == "column" && variables.wheels.class.mapping[loc.key].value == loc.property)
+ {
+ // developer has chosen to map this column to a property with a different name so set that here
+ loc.property = loc.key;
+ break;
+ }
}
- }
- loc.type = SpanExcluding(loc.columns["type_name"][loc.i], "( ");
-
- // set the info we need for each property
- variables.wheels.class.properties[loc.property] = {};
- variables.wheels.class.properties[loc.property].dataType = loc.type;
- variables.wheels.class.properties[loc.property].type = variables.wheels.class.adapter.$getType(loc.type, loc.columns["decimal_digits"][loc.i]);
- variables.wheels.class.properties[loc.property].column = loc.columns["column_name"][loc.i];
- variables.wheels.class.properties[loc.property].scale = loc.columns["decimal_digits"][loc.i];
-
- // get a boolean value for whether this column can be set to null or not
- // if we don't get a boolean back we try to translate y/n to proper boolean values in cfml (yes/no)
- variables.wheels.class.properties[loc.property].nullable = Trim(loc.columns["is_nullable"][loc.i]);
- if (!IsBoolean(variables.wheels.class.properties[loc.property].nullable))
- variables.wheels.class.properties[loc.property].nullable = ReplaceList(variables.wheels.class.properties[loc.property].nullable, "N,Y", "No,Yes");
-
- variables.wheels.class.properties[loc.property].size = loc.columns["column_size"][loc.i];
- variables.wheels.class.properties[loc.property].label = Humanize(loc.property);
- variables.wheels.class.properties[loc.property].validationtype = variables.wheels.class.adapter.$getValidationType(variables.wheels.class.properties[loc.property].type);
-
- if (StructKeyExists(variables.wheels.class.mapping, loc.property)) {
- if (StructKeyExists(variables.wheels.class.mapping[loc.property], "label"))
- variables.wheels.class.properties[loc.property].label = variables.wheels.class.mapping[loc.property].label;
- if (StructKeyExists(variables.wheels.class.mapping[loc.property], "defaultValue"))
- variables.wheels.class.properties[loc.property].defaultValue = variables.wheels.class.mapping[loc.property].defaultValue;
- }
-
- if (loc.columns["is_primarykey"][loc.i])
- {
- setPrimaryKey(loc.property);
- }
- else if (variables.wheels.class.automaticValidations and not ListFindNoCase("#application.wheels.timeStampOnCreateProperty#,#application.wheels.timeStampOnUpdateProperty#,#application.wheels.softDeleteProperty#", loc.property))
- {
- // set nullable validations if the developer has not
- loc.defaultValidationsAllowBlank = variables.wheels.class.properties[loc.property].nullable;
- if (!variables.wheels.class.properties[loc.property].nullable and !Len(loc.columns["column_default_value"][loc.i]) and !$validationExists(property=loc.property, validation="validatesPresenceOf"))
+ loc.type = SpanExcluding(loc.columns["type_name"][loc.i], "( ");
+
+ // set the info we need for each property
+ variables.wheels.class.properties[loc.property] = {};
+ variables.wheels.class.properties[loc.property].dataType = loc.type;
+ variables.wheels.class.properties[loc.property].type = variables.wheels.class.adapter.$getType(loc.type, loc.columns["decimal_digits"][loc.i]);
+ variables.wheels.class.properties[loc.property].column = loc.columns["column_name"][loc.i];
+ variables.wheels.class.properties[loc.property].scale = loc.columns["decimal_digits"][loc.i];
+
+ // get a boolean value for whether this column can be set to null or not
+ // if we don't get a boolean back we try to translate y/n to proper boolean values in cfml (yes/no)
+ variables.wheels.class.properties[loc.property].nullable = Trim(loc.columns["is_nullable"][loc.i]);
+ if (!IsBoolean(variables.wheels.class.properties[loc.property].nullable))
+ variables.wheels.class.properties[loc.property].nullable = ReplaceList(variables.wheels.class.properties[loc.property].nullable, "N,Y", "No,Yes");
+
+ variables.wheels.class.properties[loc.property].size = loc.columns["column_size"][loc.i];
+ variables.wheels.class.properties[loc.property].label = Humanize(loc.property);
+ variables.wheels.class.properties[loc.property].validationtype = variables.wheels.class.adapter.$getValidationType(variables.wheels.class.properties[loc.property].type);
+
+ if (StructKeyExists(variables.wheels.class.mapping, loc.property)) {
+ if (StructKeyExists(variables.wheels.class.mapping[loc.property], "label"))
+ variables.wheels.class.properties[loc.property].label = variables.wheels.class.mapping[loc.property].label;
+ if (StructKeyExists(variables.wheels.class.mapping[loc.property], "defaultValue"))
+ variables.wheels.class.properties[loc.property].defaultValue = variables.wheels.class.mapping[loc.property].defaultValue;
+ }
+
+ if (loc.columns["is_primarykey"][loc.i])
+ {
+ setPrimaryKey(loc.property);
+ }
+ else if (variables.wheels.class.automaticValidations and not ListFindNoCase("#application.wheels.timeStampOnCreateProperty#,#application.wheels.timeStampOnUpdateProperty#,#application.wheels.softDeleteProperty#", loc.property))
{
- validatesPresenceOf(properties=loc.property);
+ // set nullable validations if the developer has not
+ loc.defaultValidationsAllowBlank = variables.wheels.class.properties[loc.property].nullable;
+ if (!variables.wheels.class.properties[loc.property].nullable and !Len(loc.columns["column_default_value"][loc.i]) and !$validationExists(property=loc.property, validation="validatesPresenceOf"))
+ {
+ validatesPresenceOf(properties=loc.property);
+ }
+ // always allowblank if a database default or validatesPresenceOf() has been set
+ if (Len(loc.columns["column_default_value"][loc.i]) or $validationExists(property=loc.property, validation="validatesPresenceOf"))
+ loc.defaultValidationsAllowBlank = true;
+ // set length validations if the developer has not
+ if (variables.wheels.class.properties[loc.property].validationtype eq "string" and !$validationExists(property=loc.property, validation="validatesLengthOf"))
+ validatesLengthOf(properties=loc.property, allowBlank=loc.defaultValidationsAllowBlank, maximum=variables.wheels.class.properties[loc.property].size);
+ // set numericality validations if the developer has not
+ if (ListFindNoCase("integer,float", variables.wheels.class.properties[loc.property].validationtype) and !$validationExists(property=loc.property, validation="validatesNumericalityOf"))
+ validatesNumericalityOf(properties=loc.property, allowBlank=loc.defaultValidationsAllowBlank, onlyInteger=(variables.wheels.class.properties[loc.property].validationtype eq "integer"));
+ // set date validations if the developer has not (checks both dates or times as per the IsDate() function)
+ if (variables.wheels.class.properties[loc.property].validationtype eq "datetime" and !$validationExists(property=loc.property, validation="validatesFormatOf"))
+ validatesFormatOf(properties=loc.property, allowBlank=loc.defaultValidationsAllowBlank, type="date");
}
- // always allowblank if a database default or validatesPresenceOf() has been set
- if (Len(loc.columns["column_default_value"][loc.i]) or $validationExists(property=loc.property, validation="validatesPresenceOf"))
- loc.defaultValidationsAllowBlank = true;
- // set length validations if the developer has not
- if (variables.wheels.class.properties[loc.property].validationtype eq "string" and !$validationExists(property=loc.property, validation="validatesLengthOf"))
- validatesLengthOf(properties=loc.property, allowBlank=loc.defaultValidationsAllowBlank, maximum=variables.wheels.class.properties[loc.property].size);
- // set numericality validations if the developer has not
- if (ListFindNoCase("integer,float", variables.wheels.class.properties[loc.property].validationtype) and !$validationExists(property=loc.property, validation="validatesNumericalityOf"))
- validatesNumericalityOf(properties=loc.property, allowBlank=loc.defaultValidationsAllowBlank, onlyInteger=(variables.wheels.class.properties[loc.property].validationtype eq "integer"));
- // set date validations if the developer has not (checks both dates or times as per the IsDate() function)
- if (variables.wheels.class.properties[loc.property].validationtype eq "datetime" and !$validationExists(property=loc.property, validation="validatesFormatOf"))
- validatesFormatOf(properties=loc.property, allowBlank=loc.defaultValidationsAllowBlank, type="date");
+
+ variables.wheels.class.propertyList = ListAppend(variables.wheels.class.propertyList, loc.property);
+ variables.wheels.class.columnList = ListAppend(variables.wheels.class.columnList, variables.wheels.class.properties[loc.property].column);
+ loc.processedColumns = ListAppend(loc.processedColumns, loc.columns["column_name"][loc.i]);
}
-
- variables.wheels.class.propertyList = ListAppend(variables.wheels.class.propertyList, loc.property);
- variables.wheels.class.columnList = ListAppend(variables.wheels.class.columnList, variables.wheels.class.properties[loc.property].column);
- loc.processedColumns = ListAppend(loc.processedColumns, loc.columns["column_name"][loc.i]);
}
- }
-
- // raise error when no primary key has been defined for the table
- if (!Len(primaryKeys()))
- {
- $throw(type="Wheels.NoPrimaryKey", message="No primary key exists on the `#tableName()#` table.", extendedInfo="Set an appropriate primary key on the `#tableName()#` table.");
- }
-
- // add calculated properties
- variables.wheels.class.calculatedPropertyList = "";
- for (loc.key in variables.wheels.class.mapping)
- {
- if (StructKeyExists(variables.wheels.class.mapping[loc.key], "type") and variables.wheels.class.mapping[loc.key].type != "column")
+
+ // raise error when no primary key has been defined for the table
+ if (!Len(primaryKeys()))
{
- variables.wheels.class.calculatedPropertyList = ListAppend(variables.wheels.class.calculatedPropertyList, loc.key);
- variables.wheels.class.calculatedProperties[loc.key] = {};
- variables.wheels.class.calculatedProperties[loc.key][variables.wheels.class.mapping[loc.key].type] = variables.wheels.class.mapping[loc.key].value;
+ $throw(type="Wheels.NoPrimaryKey", message="No primary key exists on the `#tableName()#` table.", extendedInfo="Set an appropriate primary key on the `#tableName()#` table.");
}
- }
- // set up soft deletion and time stamping if the necessary columns in the table exist
- if (Len(application.wheels.softDeleteProperty) && StructKeyExists(variables.wheels.class.properties, application.wheels.softDeleteProperty))
- {
- variables.wheels.class.softDeletion = true;
- variables.wheels.class.softDeleteColumn = variables.wheels.class.properties[application.wheels.softDeleteProperty].column;
- }
- else
- {
- variables.wheels.class.softDeletion = false;
- }
-
- if (Len(application.wheels.timeStampOnCreateProperty) && StructKeyExists(variables.wheels.class.properties, application.wheels.timeStampOnCreateProperty))
- {
- variables.wheels.class.timeStampingOnCreate = true;
- variables.wheels.class.timeStampOnCreateProperty = application.wheels.timeStampOnCreateProperty;
- }
- else
- {
- variables.wheels.class.timeStampingOnCreate = false;
- }
-
- if (Len(application.wheels.timeStampOnUpdateProperty) && StructKeyExists(variables.wheels.class.properties, application.wheels.timeStampOnUpdateProperty))
- {
- variables.wheels.class.timeStampingOnUpdate = true;
- variables.wheels.class.timeStampOnUpdateProperty = application.wheels.timeStampOnUpdateProperty;
- }
- else
- {
- variables.wheels.class.timeStampingOnUpdate = false;
+ for (loc.key in variables.wheels.class.mapping)
+ {
+ if (StructKeyExists(variables.wheels.class.mapping[loc.key], "type") and variables.wheels.class.mapping[loc.key].type != "column")
+ {
+ variables.wheels.class.calculatedPropertyList = ListAppend(variables.wheels.class.calculatedPropertyList, loc.key);
+ variables.wheels.class.calculatedProperties[loc.key] = {};
+ variables.wheels.class.calculatedProperties[loc.key][variables.wheels.class.mapping[loc.key].type] = variables.wheels.class.mapping[loc.key].value;
+ }
+ }
+
+ // set up soft deletion and time stamping if the necessary columns in the table exist
+ if (Len(application.wheels.softDeleteProperty) && StructKeyExists(variables.wheels.class.properties, application.wheels.softDeleteProperty))
+ {
+ variables.wheels.class.softDeletion = true;
+ variables.wheels.class.softDeleteColumn = variables.wheels.class.properties[application.wheels.softDeleteProperty].column;
+ }
+ else
+ {
+ variables.wheels.class.softDeletion = false;
+ }
+
+ if (Len(application.wheels.timeStampOnCreateProperty) && StructKeyExists(variables.wheels.class.properties, application.wheels.timeStampOnCreateProperty))
+ {
+ variables.wheels.class.timeStampingOnCreate = true;
+ variables.wheels.class.timeStampOnCreateProperty = application.wheels.timeStampOnCreateProperty;
+ }
+ else
+ {
+ variables.wheels.class.timeStampingOnCreate = false;
+ }
+
+ if (Len(application.wheels.timeStampOnUpdateProperty) && StructKeyExists(variables.wheels.class.properties, application.wheels.timeStampOnUpdateProperty))
+ {
+ variables.wheels.class.timeStampingOnUpdate = true;
+ variables.wheels.class.timeStampOnUpdateProperty = application.wheels.timeStampOnUpdateProperty;
+ }
+ else
+ {
+ variables.wheels.class.timeStampingOnUpdate = false;
+ }
}
</cfscript>
<cfreturn this>
diff --git a/wheels/model/properties.cfm b/wheels/model/properties.cfm
index 727b53f..fb96b34 100644
--- a/wheels/model/properties.cfm
+++ b/wheels/model/properties.cfm
@@ -58,7 +58,7 @@
<cfargument name="column" type="string" required="false" default="" hint="The name of the column in the database table to map the property to.">
<cfargument name="sql" type="string" required="false" default="" hint="A SQL expression to use to calculate the property value.">
<cfargument name="label" type="string" required="false" default="" hint="A custom label for this property to be referenced in the interface and error messages.">
- <cfargument name="defaultValue" type="string" required="false" hint="A default value for this property.">
+ <cfargument name="defaultValue" type="any" required="false" hint="A default value for this property.">
<cfscript>
// validate setup
if (Len(arguments.column) and Len(arguments.sql))
@rip747
Copy link
Author

rip747 commented May 19, 2011

in order to use, just set table() to an empty string and it will bypass all the database inspection that wheels does, then just declare the properties you want on the model.

<cffunction name="init">
    <cfset table("")>
    <cfset property(name="emaillistid")>
    <cfset property(name="chapterid")>
    <cfset property(name="sentfrom")>
    <cfset property(name="subject")>
    <cfset property(name="message")>

    <cfset validatesLengthOf(property="sentfrom", maximum="255", message="Message is required")>
    <cfset validatesLengthOf(property="subject", maximum="255", message="Subject is required")>
    <cfset validatesPresenceOf(property="message", message="Message must be present")>

</cffunction>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment