Gateway.getByFilter - new function... OMG!

18 views
Skip to first unread message

Mark Drew

unread,
Jul 10, 2009, 11:06:44 AM7/10/09
to reacto...@googlegroups.com, reacto...@googlegroups.com
Dear Users

Hallo there, I bet you are surprised there is an email from me right? Anyway, I have been busy adding a little feature that should make using  gatewauys and object oriented quieries a tad easier :) 

Easier you say? I am sure you want some of that action right? So, what I have added to a gateway is the ability to do the following, lets start with a big example of code and work our way through it. So imagine you have a table of blog posts, and you want to get all the published ones, in order of date, and you want to paginate it.... how would you do this with reactor currently:

1) Create a custom function in your gateway
2) Go nuts with the Object Oriented Queries.

I say NO, dont do that! Do this:


Posts = reactor.getByFilter(
include={siteid=1, approved=1},
exclude={deleted=1},
contains={title="elvis"}
orderby={publicationDate="DESC"},
page=1,
rows=10
);


So what am I doing here? Well its actually rather easy...  the get by filter takes in a number of structs and variables, the include, excliude and contains structs are key value pairs of things to include (Where X=Y), the things to exclude is another struct of things to exclude and the contains is to search for , in this example where title has the word "elvis" in it

The order by close you can add various fields with either DESC and ASC on it to order it

you can also paginate results! yes! you have to just ask which page you want to get and how many rows per page you want.

Careful tho, the pagination only works natively with mySQL, I have added code that makes it work across databases but it fakes it for other db's by doing a cfloop

what else is there?

Oh yeah, well it normally returns a query by default, but I am sure you might want to do format="iterator" If you want to return an iterating business object, this is kinda flakey as I want to just return an iterator object but didnt have time to really look into ti

I have checked it all in so hopefully it should make your working life easier. 

Regards

Mark Drew


Mischa Sameli

unread,
Aug 26, 2009, 7:21:53 AM8/26/09
to Reactor development
Hi Mark
What about the addition for the isIn-clause? I use this clause quite
often and I think it's a very common case.
Greetings from Switzerland
--
Mischa Sameli
Senior Developer at backslash - artists of new media
Work: http://www.backslash.ch
Blog: http://www.samelis.ch/blog/mischa/

Mark Drew

unread,
Aug 26, 2009, 10:32:51 AM8/26/09
to reacto...@googlegroups.com
Try adding it, its in the abstractGateway

I have added a bunch of stuff, that I am finishing off, some of the
things are injectors, so you can load classes into your Record and
Gateway (hell and even Metadata) objects from coldspring (if you are
using that to configure reactor)

The other thing I am adding is the ability to render a form from an
object easily, in other words, you can do scaffolding into your
application pretty easily

MD

Mischa Sameli

unread,
Aug 26, 2009, 11:03:43 AM8/26/09
to Reactor development
Well, that's what I've done locally, based on the latest svn-code:
I've modified the function getByFilter() so that the values passed
through the arguments include and exclude can contain valueList. I
assumed that the delimiter has to be a comma, because if you would
pass the expression directly to a database, the In-Clause would also
be a comma-separated value-list.

<!--- Do the includes --->
<cfloop collection="#include#" item="inc">
<cfif Find(",", include[inc])>
<cfset Where.isIn(TableName, inc, include[inc])>
<cfelse>
<cfset Where.isEqual(TableName,inc, include[inc])>
</cfif>

</cfloop>

<!--- Do the excludes --->
<cfloop collection="#exclude#" item="ex">
<cfif Find(",", exclude[ex])>
<cfset Where.isNotIn(TableName, ex, exclude[inc])>
<cfelse>
<cfset Where.isNotEqual(TableName,ex, exclude[ex])>
</cfif>
</cfloop>

The downside of this hack is that you can't pass expressions
containing commas anymore to the include and exclude filters anymore
to get exact matches. What Do you mean? Would it be better do add more
arguments to the function? The dissadvantage of this option would be
the naming of the arguments, because include und contains are already
reserved, so there are not many meaningful options left...
Cheers
Mischa

Tom Chiverton

unread,
Aug 26, 2009, 11:11:11 AM8/26/09
to reacto...@googlegroups.com
2009/8/26 Mischa Sameli <mischa...@gmail.com>:

> I've modified the function getByFilter() so that the values passed
> through the arguments include and exclude can contain valueList. I

Rather than overloading the arguments to the include and exclude
'tags', do people think it would be clearer and less of a possible
gotcha to instead have includeIfInList / excludeIfInList tags ?

--
Tom

Mark Drew

unread,
Aug 26, 2009, 11:20:35 AM8/26/09
to reacto...@googlegroups.com
What do you mean tags?

I think this is a massive filter function, and as we increase we could
do something like unite all the requirements into something like a
params.

So you would create your filter:
<cfscript >
filter = {
include = {},
exlude = {}
isIn = {column="valuelist"},
etc..

}
</cfscript>

and you would pass it in, rather than doing all the actual attributes.

MD

Tom Chiverton

unread,
Aug 26, 2009, 11:35:09 AM8/26/09
to reacto...@googlegroups.com
2009/8/26 Mark Drew <mark...@gmail.com>:

> What do you mean tags?

Well, whatever the parts of a filter are called ...
But yes, use isIn/isNotIn rather than overloading the existing ones. I
may have time to hack on this tonight.

> and you would pass it in, rather than doing all the actual attributes.

That would work now, though, right ? Got another (fuller ?) example ?

--
Tom

Tom Chiverton

unread,
Aug 29, 2009, 9:50:33 AM8/29/09
to reacto...@googlegroups.com
> But yes, use isIn/isNotIn rather than overloading the existing ones. I
> may have time to hack on this tonight.

r465 should add isIn and notIn as possible filter clauses.
Cheers for the idea Mischa, it was a good one.

--
Tom

Mischa Sameli

unread,
Aug 29, 2009, 10:20:29 AM8/29/09
to Reactor development
thanks a lot for integrating it - will help me a lot.
Cheers
Mischa
Reply all
Reply to author
Forward
0 new messages