[Dbix-class] "table" methods in schemas?
Brandon Black
blblack at gmail.com
Tue Jan 31 22:58:11 CET 2006
I know we covered this on irc at some point, but I'm still at a loss
as to the "correct" way to do these types of things.
For example, in the old/bad way of doing things, a table-class
MyDB::FooTable might have a method like:
sub get_event_data {
my $class = shift;
my $eventid = shift;
my $data = [];
my $rs = $class->search({ evid => $eventid });
while(my $row = $rs->next) {
# even dumber, update row data when viewed :)
$row->seen = $row->seen + 1;
$row->update();
push(@$data, { x => ($row->x || 22), ... });
}
return $data;
}
And in an application I might do:
MyDB::FooTable->search(....);
or sometimes
MyDB::FooTable->get_event_data(43);
The idea being that this class method encapsulates a certain usage of
the method ->search() and a common transformation of the output (and
possibly other linked actions). In some ways it is conceptually
similar to an inflate_column kind of thing, but more like
inflate_results. The reason it's inside the table class instead of
just a common subroutine within the application is because the
functionality offered isn't really application or representation
-specific, it has general applicability for anyone who might use my
Schema (much like a Stored Procedure in that way).
If I make this a class method in a Schema "package My::Schema::Foo;
sub get_event_data { ... }", can I call it as
"$schema->class('Foo')->get_event_data(43);", and will the
$self->search() within this class method even work correctly (or would
it need to be like $self->schema->resultset('Foo')->search())?
I'm lost :) I don't mind refactoring/rewriting any table-method code
I'm currently using in my old /bad way of doing things in order to do
things the Schema way, I'm just not sure where such things should land
or how. Conceptually, to me, they belong in/with whatever class
defines this table.
-- Brandon
More information about the Dbix-class
mailing list