[Catalyst] how to reuse Catalyst Schema for non-web purposes?
Kieren Diment
diment at gmail.com
Thu Apr 23 04:01:28 GMT 2009
On 23/04/2009, at 1:48 PM, kakimoto at tpg.com.au wrote:
> Hello Kieren,
>
> thank you for the link,
> http://blog.hide-k.net/archives/2008/10/dbixclassresult_1.php.
>
>
> Please correct me if I am wrong.
> Say if I have a Schema file, Schema/Subscriptions.pm
>
> After the section, "DO NOT MODIFY ANYTHING ABOVE THIS! ",
> I start declaring functions (not methods).
>
>
> sub is_trial_period
> {
> # evaluate if trial period is over or not
> # as this has to be determined through a set
> # of business rules.
> ...
> }
>
> sub is_promo_period
> {
> # evaluate if promo period is still on or not
> # as this has to be determined through a set
> # of business rules.
> ...
> }
>
>
Here's some code I have lying around:
use strict;
use warnings;
package Zotero::Schema::Collections;
use base 'DBIx::Class';
__PACKAGE__->load_components("Tree::AdjacencyList","Core");
__PACKAGE__->table("collections");
__PACKAGE__->resultset_class('Zotero::ResultSet::Collections');
__PACKAGE__->add_columns(
"collectionid",
{ data_type => "INTEGER", is_nullable => 0, size => undef },
"collectionname",
{ data_type => "TEXT", is_nullable => 0, size => undef },
"parentcollectionid",
{ data_type => "INT", is_nullable => 0, size => undef },
);
__PACKAGE__->set_primary_key("collectionid");
__PACKAGE__->has_many(
"collections",
"Zotero::Schema::Collections",
{ "foreign.parentcollectionid" => "self.collectionid" },
);
__PACKAGE__->has_many(
"collectionitems",
"Zotero::Schema::Collectionitems",
{ "foreign.collectionid" => "self.collectionid" },
);
# Created by DBIx::Class::Schema::Loader v0.04004 @ 2008-08-04 20:07:34
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:wOyyez2QNjtYXdJsmFHQFQ
__PACKAGE__->parent_column("parentcollectionID");
1;
package Zotero::ResultSet::Collections;
use warnings;
use strict;
use base qw/DBIx::Class::ResultSet/;
sub get_collections_tree {
my $self = shift;
my @res;
my $rs = $self->search( {parentcollectionid => undef},
{order_by => [ 'collectionname' ] },
);
while (my $node = $rs->next) {
push @res, $self->get_children($node);
}
return \@res;
}
sub get_children {
my ($self, $node ) = @_;
my $res = {};
$res->{title} = $node->collectionname ;
$res->{key} = $node->collectionid;
my @kids = $node->children;
$res->{expand} = 1;
$DB::single=1;
if (@kids) {
my @children;
foreach (@kids) {
push @children, [$self->get_children($_)];
}
$res->{children} = \@children;
}
return $res;
}
1;
More information about the Catalyst
mailing list