[Dbix-class] load_classes and common base class
Ash Berlin
ash_cpan at firemirror.com
Sat Apr 21 16:39:51 GMT 2007
Bill Moseley wrote:
> On Sat, Apr 21, 2007 at 03:19:14PM +0100, Ash Berlin wrote:
>> The way I do things is;
>
> Ok, so your result sources just inherit from a base class.
>
>
> Ok, so this has me wondering:
>
>> # $self->resultset_class('My::ResultSet');
>
> I noticed that in the cookbook. Are you using a single class to
> define commonly used result sets? Seems like one would want custom
> result sets to be specific to a given table class.
In the case where i use the above, I actually just use that as a base
class for a group of related sources - in my case tables that store test
results. Since each test needs slightly different data to be stored, as
well as common data, i do it that way.
>
> Where is resultset_class documented?
find . -name *.pm -or -name *.pod | xargs grep -H resultset_class | less
...
./lib/DBIx/Class/ResultSource.pm:=head2 resultset_class
There
>
> I think an example would help clear things up for me quite a bit:
>
>
> In CDBI I create extra methods in the table classes as needed to
> extend those classes. Sometimes these are instance methods and other
> times class methods.
>
>
> For example, I have the common setup where when someone signs up for
> an account they get sent a confirmation email that they must use to
> complete the registration process.
>
> I have a table account_confirmation that tracks these pending accounts.
> I also have a users table (called "person") where I store their signup
> data.
>
> People will sign up for an account, but never complete the process by
> responding to the account confirmation email, so I have to
> periodically delete those pending accounts.
>
> In CDBI I have a class methods that does that:
>
> App::AccountConfirmation->clean;
>
> And a feature (or problem) with CDBI is that the $dbh can be found
> from the class using Foo->db_Main. Don't need to pass $schema in, for
> example.
>
> That means I can do:
>
> package AccountConfirmation;
>
> sub clean {
> my $self = shift;
>
> # Deleting a person will cascade delete account_confirmation
>
> my $sql = <<'';
> delete from person where id in
> (
> SELECT
> p.id
> FROM
> person p
> JOIN account_confirmation a ON (p.id = a.person)
> WHERE
> a.created_time < now() - interval '1 day'
> )
>
> return $self->db_Main->do( $sql );
> }
>
> So, how do I implement something like that in DBIC?
>
>
> Is this where ResultSetManager comes in?
>
>
Could be done with RSM yes, or use resultset_class and make it a method
on that class. Then:
# if using RSM
# sub clean: ResultSet {
sub clean {
my ($rs) = @_;
# This might not be exactly right mind...
$rs->search(
{'account_confirmation.created_time' => \'< now() - interval '1
day''},
{ join => 'account_confirmation'}
)->delete();
}
Make more sense?
More information about the Dbix-class
mailing list