[Dbix-class] RestrictWithObject and ResultSet

Jonathan Rockway jon at jrock.us
Tue Mar 4 08:20:06 GMT 2008


* On Mon, Mar 03 2008, James R. Leu wrote:
> Hello,
>
> I'm trying to use RestrictWithObject in my Catalyst app
> to filter all of the results of queries to my Model by
> checking if their keys are in hash that I put inside the
> 'Restricting Object'.  I've successfully gotten the
> 'restrict_Foo_resultset' to be called, but I'm stuck
> when trying to build a result set to return.
>
> I have some logic that sift through each row if the $fullset
> and then tries to build a new $filterset to return.  No matter
> how I build the $filterset, the app is getting access
> to all of the rows in the $fullset.
>
> Here is a my latest attempt at the restricting method:
>
> sub restrict_Foo_resultset {
>     my $self = shift;
>     my $fullset = shift;
>     my $class = $fullset->result_class();
>
>     my @all;
>     while (my $item = $fullset->next()) {
>         if (defined($self->{hash}->{$item->id()})) {
>             push(@all, $item);
>         }
>     }
>
>     $fullset->reset();
>     $fullset->set_cache([@all]);
>     return $fullset;
> }

I don't think you should try to turn a bunch of row objects into a
ResultSet.  That's not really what a ResultSet is (it's a "promise to
return row objects", not a container for row objects).

So basically you need to turn your hash-checking into a SQL query.
Otherwise, RestrictWithObject might not be the right approach.

example:

  sub restrict_Task_resultset {
     my ($self) = @_;
     my $user = some user object;

     # $self is a Tasks resultset.  the return value is tasks belonging
     # to $c->user
     return $self->search({ owner => $user->id });

     # optionally "return $user->related_resultset('tasks')" would work
  }

The alternative is:

  sub return_some_shit {
     my ($self) = @_;
     return @some_shit;
  }

There is nothing wrong with that.

Regards,
Jonathan Rockway



More information about the DBIx-Class mailing list