ColdFusion with ORM init function setting values in a none persistent property - coldfusion

Why does this not work in ColdFusion with ORM? I don't get any error but getval2() is blank
property name="ID" type="string" fieldtype="id" generator="guid";
property name="val1" type="string" ormtype="string" persistent=true;
property name="val2" type="any" persistent=false default="";
public statsEntity function init(){
variables.val2= this.getval1();
return Super.init();
}
what I think should happen is getval2() should be the value of val1.
Any ideas why this is not the case?
ORMExecuteQuery("from myTable")

Not sure I understand correctly, but init() only runs on new enitity so will NOT work here try postLoad() not init().

Related

WSO2 Call Template Mediator how to get a variable

I'm trying to use Call Template Mediator in WSO2 and I'm trying to get a dynamic value inside function, but I don't find the way to get it.
For example:
<property expression="$ctx:variable" name="test" type="STRING"/>
<call-template target="HelloWorld_Logger">
<with-param name="message" value="VARIABLE: " expression="$ctx:test" />
</call-template>
I'm not able to get the property "test", if i remove value field the IDE reports an error.
There is some way to get a property inside <call-template> function?
The Call Template Mediator doesn't have a parameter called expression to pass dynamic values. You must pass the XPath expression within {} to the value parameter itself. In your case, the call-template will be as follows,
<call-template target="HelloWorld_Logger">
<with-param name="message" value="{$ctx:test}"/>
</call-template>
Please refer to the Call Template Mediator for more information.

How to set entity as unique Key in ColdBox

I'm creating a model entity in ColdBox.
component persistent="true" {
property name="id" type="int" fieldType="id" generator="increment";
property name="stamptime" type="timestamp";
property name="type" type="string" length="1" sqltype="varchar(1)";
property name="serial" type="string" length="100" sqltype="varchar(100)";}
the id field is set as identity and as primary key. the problem is i want to set serial field as unique key.. is there any way to set this field as unique key?
Have you tried defining in the property as follow:
component persistent="true" {
property name="serial" type="string" length="100" sqltype="varchar(100)" unique="true";
// and / or as a validation via constraints?
this.constraints = {
serial = { unique=true };
} //constraints
} //component

Skip to sending parameter in ColdFusion REST Service

I was writing a REST component but having difficulties when I do not want to pass argument for a parameter. My code and URL (PATH style) look like this.
Component definition:
<cfcomponent rest="true" restPath="SearchRestAPI">
<cffunction name="restaurantResults" access="remote" httpMethod="get"
output="false" returntype="Query" produces="application/json"
restPath="Search/{city : ([a-zA-Z])*}/{address}/{type}">
<cfargument name="city" default="" type="string" restargsource="path">
<cfargument name="address" default="" type="string" restargsource="path">
<cfargument name="type" default="" type="string" restargsource="path">
Calling URL/Path to consume the REST service. Notice I do not want to send any value for type parameter so there is double slash at the end of the path:
http://localhost:8502/backend/section/SearchResAPI/Search/Calcutta/Lindsay Street//
But this is throwing a "Not Found" error. Any suggestion would be highly appreciated.
I am not sure about this ColdFusion functionality, but looks like "restPath" works with regular expressions (which you are already using when capturing the city parameter
([a-zA-Z])* // your city parameter matches the specified character sets zero or more times.
Similarly to the above, you could try:
restPath="Search/{city : ([a-zA-Z])*}/{address}/{type: ([a-zA-Z])*}"
Here is a ColdFusion page that has some examples as well (search the page for "restPath"):
https://wikidocs.adobe.com/wiki/display/coldfusionen/cfcomponent

ORM EntitySave() - ids for this class must be manually assigned before calling save()

When attempting to do an EntitySave("publications",arguments); .. I receive the following error.
ids for this class must be manually assigned before calling save(): publications
I can't work out why.. My database primary keys are set up correctly, and I have setter=false these properties in my CFC. I have found a bit on this error doing a Google search, but nothing seems to indicate what is causing my issue here.
Here are my CFC's. Any pointers on what I might be doing wrong are appreciated. Thanks heaps in advance!
Publications.cfc
component persistent="true" table="publications"
hint="Publications"{
property name="id" fieldtype="id" setter="false";
property name="typeid" omrtype="int";
property name="name" ormtype="string";
property name="dateScheduled" ormtype="date" ;
property name="tstamp" ormtype="date";
property name="Article" fieldtype="one-to-many" cfc="publicationArticles" fkcolumn="publicationid";
}
publicationArticles.cfc
component persistent="true" table="publicationArticles"
hint="Publications"{
property name="id" fieldtype="id" setter="false" ;
property name="typeid" ormtype="int";
property name="title" ormtype="string" ;
property name="status" ormtype="boolean";
property name="publication" fieldtype="many-to-one" cfc="publications" fkcolumn="publicationid" ;
}
publicationTypes.cfc
component persistent="true" table="publicationTypes"
hint="Publicatin Type - Lookup"{
property name="id" fieldtype="id" setter="false" ;
property name="description" ormtype="string";
property name="publications" fieldtype="one-to-many" cfc="publications" fkcolumn="typeid" ;
}
You need a generator on the property.
property name="id" fieldtype="id" generator="identity";

Testing for existence of FORM scope / struct in ColdFusion

Problem: When requesting the WSDL for a CFC, I get the following error: Variable FORM is undefined. It happens in this line of code, in the OnRequestStart method in application.cfc
<cfif structKeyExists(form,'resetappvars')>
<cfset OnApplicationStart() />
</cfif>
If I request a specific method, it works fine. I have considered using cfparam to create a default form struct if none exists, but that seems like an ugly hack and I worry it will actually create the form struct in the variables or this scope of the CFC. Maybe this is a legitimate bug as well?
Note: This only happens when I request the WSDL, if I invoke a method directly - the code executes as expected without problems.
Update: Application.cfc code sample - just add any CFC to your app and request it with ?wsdl to see the issue. This has been tested (and failed) on ColdFusion 7 and ColdFusion 8.
<cfcomponent output="false">
<cffunction name="OnApplicationStart" access="public" returntype="boolean" output="false" hint="Fires when the application is first created.">
<cfset application.dsn = "my_dsn" />
<cfreturn true />
</cffunction>
<cffunction name="OnRequestStart" access="public" returntype="boolean" output="false" hint="Fires at first part of page processing.">
<cfargument name="TargetPage" type="string" required="true" />
<cfif structKeyExists(form,'resetappvars')>
<cfset OnApplicationStart() />
</cfif>
<cfreturn true />
</cffunction>
</cfcomponent>
Maybe try adding a:
<cfif IsDefined("form")>...</cfif>
around the above code?
You could also cfparam the variable you're looking for then just change your logic a little (assuming resetAppVars is a boolean:
<cfparam name="form.resetAppVars" default="false" />
...
<cfif form.resetAppVars>
<cfset OnApplicationStart() />
</cfif>
Edit: I'm not sure if the above code could be considered a hack, but it seems pretty standard CF, to me.
This post of Ben Nadel gives detailed list of scopes available for different types of requests.
By reading it you can easily find out that form scope is not available in given context, but url is.
I've heard it's just a matter of opinion, but it seems to me that it is improper to reference your form scope within a CFC, as there is no guarantee that the form scope will be available when your cfc is invoked and when your method is called. It is better to ensure that any data that needs to be available to the method is provided explicitly to your object. This can be done either by including an argument:
<cfargument name="resetAppVars" type="boolean" required="false" default="false" />
Then you check arguments.resetAppVars, and it is always defined, but defaulted to false.
Or by creating an attribute on your object and creating an explicit set method:
(at the top of your cfc)
<cfset this.resetAppVars = false />
<cffunction name="setResetAppVars" access="public" returnType="void" output="false">
<cfargument name="flagValue" type="boolean" required="true" />
<cfset this.resetAppVars = arguments.flagValue />
</cffunction>
In which case you will check against this.resetAppVars. You can also scope this locally using <cfset var resetAppVars = false /> as the declaration, which makes it a private attribute of your object, and is probably proper, so code that invokes the object cannot improperly overwrite this variable with a non-boolean type. In that case, you would simply refer directly to resetAppvars in your test, instead of using this scope.
You could also do this:
<cfif NOT isSoapRequest()>...
and stick your remaining logic inside that chunk.