[Catalyst] Loading CRUD for all tables
Marcus Ramberg
marcus at thefeed.no
Mon Apr 11 21:57:28 CEST 2005
Right.
It is an interesting approach, unfortunately it *will* fail for cat5.
However, it would be simple to write a dispatcher that does something
similiar. Something like
my %db_class;
sub default : Private {
my ($self,$c,$table) = @_;
%db_class=map {$_->table,$_ }
$c->comp("MyApp::Model::CDBI")->loader->classes
unless scalar keys %db_class;
if ($db_class{$table}) {
$c->form( optional => [ $class ] );
$c->forward($db_class{$table});
} else {
$c->res->output ($table. ' not found');
}
}
Of course, this was written in like 3 minutes, and isn't optimal or even
likely to work, but you get the idea...
Marcus
Michael Reece skrev:
> That is an interesting approach. I have some questions:
>
> Does this mean the table actions are not available on the first request,
> since they won't get set up until there is a request for the default action?
>
> I find that to run more than one Catalyst application under mod_perl, I have
> to __PACKAGE__->actions( {} ); before setting up my actions, or else they
> get their actions confused and things start failing. In that scenario, if
> it also requires a request for the default action to set up the other
> actions, wouldn't it get into a situation where the table actions are never
> available?
>
> And, in Cat5, where the actions are subroutines instead of defined via
> __PACKAGE__->action, would something like this even be possible?
>
> And, from what I've seen (on this list) of Cat5's actions, they can't be
> defined dynamically ..
>
> On 4/11/05 6:00 AM, "Frank Wiegand" <frank.wiegand at gmail.com> wrote:
>
>
>>Hi,
>>
>>sometimes when developing a new app, using CatMCDBI::CRUD can be tedious.
>>
>>Instead of writing
>>
>> __PACKAGE__->action(
>> $table => sub my ( $self, $c ) = @_;
>> $c->form( optional => [ $class ] );
>> $c->forward($class);
>> );
>>
>>for all of my tables, I just define these actions dynamically in my
>>app's '!default' action:
>>
>> __PACKAGE__->action(
>> '!default' => sub {
>> my ( $self, $c ) = @_;
>> for my $class ( $c->comp("MyApp::Model::CDBI")->loader->classes ) {
>> __PACKAGE__->action(
>> $class->table => sub {
>> my ( $self, $c ) = @_;
>> $c->form( optional => [ $class ] );
>> $c->forward($class);
>> }
>> );
>> }
>>
>> # ... more default stuff goes here
>> }
>> };
>>
>>Maybe this is helpful for somebody ...
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: marcus.vcf
Type: text/x-vcard
Size: 282 bytes
Desc: not available
Url : http://lists.rawmode.org/pipermail/catalyst/attachments/20050411/f910fde3/marcus.vcf
More information about the Catalyst
mailing list