[Dbix-class] DBIx and persistence or object caching?

Brandon Black blblack at gmail.com
Thu May 24 17:27:15 GMT 2007


On 5/24/07, Steve Francia <steve at takkle.com> wrote:
> Since all doesn't take any parameters, but search does you can do what I am asking for with search.
>
> Add the prefetch to search while searching for nothing and then apply the all.
>
> $c->model('vidDB::video')->search( {}, { prefetch => [qw/ owner /] } )->all();
>
> Seems like a roundabout approach, is there a better way to do it?
> Is there any reason the all method doesn't accept parameters?
>

It may seem roundabout in your particular case, but it makes the most
sense for the overall API as it stands.

The reason ->all doesn't take parameters is because resultset objects
are multi-roled, so to speak.  On the one hand, a resultset object is
an abstraction of a searchable collection of objects of a given type
(or if you prefer, a searchable collection of rows in a table).  Using
->search() -like methods on a resultset produces another resultset
with further refinements, but does not hit the database and/or
actually instantiate any objects (fetch any rows).

On the other hand, a resultset object can also be used to realize this
abstraction (execute the SQL representation of the built-up
parameters) and iterate the actual objects/rows, via the
iteration-related methods ->first(), ->next(), ->all(), which return
actual objects (or an array of objects in the case of all), rather
than another resultset.

With 20/20 hindsight, it's easy to say now that the
realization/iteration part may have been better as something separate,
as in "my $realized_resultset = $resultset->realize();", and have the
iterator/all/count/etc stuff happen on those objects, and then perhaps
provide some convenience methods, like "->all()" on resultsets that
instantiate a temporary realized_resultset for one call and do ->all()
on it.

I guess in either case we could, in theory, patch $rs->all() to take
search arguments and have it do the search internally, but I like the
way the call looks now in your example.  Perhaps that's just because
I've gotten very used to the resultset concept.

-- Brandon



More information about the Dbix-class mailing list