Custom object queries in ZoomFlex
This article looks at how you can use custom queries in ZoomFlex to return data from the server-side objects.
In the generated ZoomFlex code under the 'events' folder is a [classname]GetObjectsEvent.as file. This event has 3 properties which are columns,fieldName and fieldValue. This means when you dispatch the event you can pass in the columns you want to return from the database, the field name and field value which, in SQL terms would look something like
select columns from myclasstable where fieldname = fieldvalue
This is fine if you are only filtering on one field / value pair, but what happens if you want to filter on multiple name pair values?
The answer is pretty easy and is a quick 5 minute job. You simply need to add a new property to the event, add the relevant arguments to the command and delegate and do a quick change to the delegate getVOsByFilter function.
Here are the steps
Open the generated [classname]GetObjectsEvent.as file
Add an additional property called whereClause
public var columns : String = "";
public var fieldName : String = "";
public var fieldValue : String = "";
public var whereClause : String = "";
Also update the other functions in the event
public function RssGetObjectsEvent(eventID:String, fieldName:String="", fieldValue:String="", whereClause:String="",columns:String="LABEL,OBJ_UUID,CREATED", handlers:Callbacks=null) {
super(eventID, handlers, true, true);
this.fieldName = fieldName;
this.fieldValue = fieldValue;
this.columns = columns;
this.whereClause = whereClause;
}
override public function clone():Event {
return new RssGetObjectsEvent(this.type).copyFrom(this);
}
override public function copyFrom(ev:Event):Event {
// copy custom attributes
if (ev is RssGetObjectsEvent){
this.columns = (ev as RssGetObjectsEvent).columns;
this.fieldName = (ev as RssGetObjectsEvent).fieldName;
this.fieldValue = (ev as RssGetObjectsEvent).fieldValue;
this.whereClause = (ev as RssGetObjectsEvent).whereClause;
}
return super.copyFrom(this);
}
Now open the command/[classname]Command.as
Find the getVO'sByFilter function and add the whereClause argument
public function getVOsByFilter(event:RssGetObjectsEvent):void {
var handlers : Callbacks = new Callbacks(onResults_getVOsByFilter,this.onFault);
var delegate : RssDelegate = new RssDelegate(handlers);
delegate.getVOsByFilter(event.fieldValue,
event.fieldName,
event.columns,
__model.blockSize,
__model.maxRows,
__model.startRow,
event.whereClause);
}
Now open the delegates/[cassname]Delegate.as
Find the getVOsByFilter function and add the whereClause argument
public function getVOsByFilter( filterValue: String,
filterName : String,
columns : String,
blockSize : int,
maxRows : int,
startRow : int ,
whereClause:String):void {
find where the whereClause is passed as an argument to the server in the getVOsByFilter function and set it to the value of the passed in whereClause argument
oArgs["whereClause"] = whereClause;
Now in your view you can add the code to dispatch the event
dispatchEvent(new RssGetObjectsEvent(RssGetObjectsEvent.GETBYFILTER,"","","label='x' and created = 'test'","*",null));
As you can see I am filtering on label="x" and created="test" as well as returning all columns by using the "*" in my select clause.
To test the event is dispatching correctly go to your application and run it so that the event dispatches (i'm presuming you are having the user interact with an input such as a button). Open the ZoomFlex debug screen and look for the event. If you click on the event you should see your whereClause being pased to the server.
-------------Remote Object Arguments---------------------------
Value:
whereClause : label='x' and created = 'test'
blockSize : 200
className : com.a3.model.vo::RssVO
useBlockFactor : true
selectFields : *
startRow : 1
filterValueText :
filterValuefilterName :
Comments
I guess there is always the ColdFusion route but would be nice to just do it from Flex with this dispatchEvent.
"Ordering is done by two arguments. The first is orderBy which is the column(s) to order by. This defaults to label. The second is orderDir which specifies whether to order ascending (asc) or descending (desc). This defaults to asc. Multiple columns can be specified in orderBy, however if you need better control over the ordering, use the argument orderClause, which allows you to specify a complete ordering clause, excluding the command, ORDER BY, which may be db specific."
# Posted by: Arnold Young | 17 May 2008 | 12:34 AM
Arnold, you seem to have answered your own question I think. You simply follow the same steps for adding a whereclause to add an orderby and orderdir argument to the filtered VO event.
# Posted by: Grant Strake | 17 May 2008 | 12:44 AM
print page

Thanks - this is great! Can we extend this to have an orderBy Property also? That way we would have three main parts of a select statement covered here -- SELECT, WHERE, and ORDER BY with this extended dispatchEvent? The sorting in Flex seems to be more code than needed like 7 lines where an ORDERBY would handle it much simpler. For instance, it is quite involved sorting date/time descending properly in a Flex array collection.
# Posted by: Arnold Young | 16 May 2008 | 03:53 PM