[Catalyst] Search example in any docs?

Zbigniew Lukasiak zzbbyy at gmail.com
Wed Sep 24 10:26:27 BST 2008


On Tue, Sep 23, 2008 at 10:21 PM, Dr. Jennifer Nussbaum
<bg271828 at yahoo.com> wrote:
>
> --- On Tue, 9/23/08, Zbigniew Lukasiak <zzbbyy at gmail.com> wrote:
>
>> Hi there,
>>
>> I am sure you are referring to
>> http://www.catalystframework.org/calendar/2007/16 :)  I
>> don't really
>> know what you need, but I start that article with a very a
>> simple
>> solution:
>>
>> my @records = $schema->ResultSet( 'MyTable'
>> )->search(
>>         $reqest->params,
>>         { page => 1, rows => 5 }
>>     );
>>
>> Only after presenting that I start discussing what to do
>> when you need
>> to add some more complex predicates to the query and
>> propose a
>> solution to that.
>
> Yes i did see that and i did try it, but i quickly found that i ran into all sorts of problems. For example my params would usually include something
> like a "action" param with the value "Submit query", whihc doesnt work so well when it hits the database ;-)

Perhaps something like: $params = $c->req->params; delete $params->{action}; ?

You further talk about the 'LIKE' queries etc.  It must be my writing
skills or maybe I did a too big step between the simple solution and
the complex ones - but answering the question on how to extend the
basic search in a clean way was exactly the point of my article.  If
you ask this question then I must have failed in my goal - so excuse
my persistance but I would like to persue it a bit further.

So here is a simple search with a 'LIKE' operator on mycolumn.

sub search_for_mycolumn {
        my ( $self, $params ) = @_;
        my %search_params = { mycolumn => { 'like' => '%' .
$params->{my_column} . '%' };
        $self = $self->search( \%search_params );
        return $self;
}

The key is that advanced_search will look for methods called
search_for_* and use them automatically for the search.   You can
combine it with the exact matches (that you don't need to write any
additional method for) and any other complex predicates.  The full
advantage of this technique might not be visible for simple cases like
this one - but I am sure that as soon as you add tags into that you'll
find it cleaner.

Now I think I should have written a full example - so that the reader
would see how the parts need to be assembled.

So your ResultSet class would be something like:

use base AdvancedSearch;

sub search_for_mycolumn {
        my ( $self, $params ) = @_;
        my %search_params = { mycolumn => { 'like' => '%' .
$params->{my_column} . '%' };
        $self = $self->search( \%search_params );
        return $self;
}
... (other complex searches here) ...
1;

And the code in your controller would be similar to:

my $params = $c->req->params;
delete $params->{action};
my $result_set = $c->model( 'MyModel')->ResultSet( 'MyTable' );
my @records = $result_set->advanced_search(
        $params,
        { page => 1, rows => 5 }
    );


 Maybe an update would fit for the next Advent Calendar?

Cheers,
Zbigniew



More information about the Catalyst mailing list