[Catalyst] best way to create a flexible, multiple key, CRUD application?

Mark Zealey mark at itsolve.co.uk
Sun Feb 18 21:28:42 GMT 2007


I looked at the crud modules but i could never really get them to work; they 
were always rather too limited for me and if you ever want to extend it 
slightly; you will have to rewrite it all yourself. It's easy enough to do a 
simple crud app yourself; you want to use DBIx::Class probably with the 
Schema::Loader module at least during development. use the FormBuilder 
controller base class, with .fb file like:

name: invite
method: post
fields:
    name:
        label: Name
        required: 1
    number:
        label: Number of people
        validate: INT
        value: 1
        required: 1
    address:
        label: Postal address
        type: textarea
        rows: 6
        cols: 40
        required: 0
    email:
        label: Email Address
        required: 0
    coming:
        label: Coming?
        options: 1=Yes, 0=No
        required: 0


 and then just write a little glue code; something like:

package Zealey::Controller::Invite;
use strict;
use warnings;

use base 'Catalyst::Controller::FormBuilder';

sub index : Private {
    my ($self, $c) = @_;
    $c->res->redirect($c->uri_for('view'));
}

sub add : Local Form('/invite/main') {
    my ($self, $c) = @_;

    my $form = $self->formbuilder;

    $form->submit('Add new invite');

    if($form->submitted and $form->validate) {
        $c->model('Zealey::Invite')->create($form->fields)
        $c->res->redirect($c->uri_for('view'));
    }
}

sub update : Local Form('/invite/main') {
    my ($self, $c, $id) = @_;

    my $form = $self->formbuilder;

    $form->submit('Update invite');

    my $db = $c->model('Zealey::Invite')->find($id) or die "$id not found";

    if($form->submitted) {
        if ($form->validate) {
            $db->set_columns($form->fields);
            $db->update;
            $c->res->redirect($c->uri_for('view'));
        }
    } else {
       # Fill in form
        $form->field( name => $_, value => $db->$_ ) for @DB_COLS
    }
}

sub delete : Local {
    my ($self, $c, $id) = @_;

    my $db = $c->model('Zealey::Invite')->find($id) or die "Not found";
    $db->delete;
    $c->res->redirect($c->uri_for('view'));
}

sub view : Local {
    my ($self, $c) = @_;

    $c->stash->{rows} = [ $c->model('Zealey::Invite')->all ];
}

1;

I use a View::TT module; you just stick [% FormBuilder.render %] in the 
add/update files; and some sort of table renderer for each column in the view 
one iterating over the 'rows' variable.

Admittedly; this is probably 50% more complicated than setting up InstantCRUD 
or the HTML::Widget::DBIC module, but it is very easily extendable as it is 
not really tied to any particular framework - it would be trivial for example 
to change it to link across 2 tables by simply issuing the create/update 
statements and using foreign key constraints. It's much less black-magic'y; 
it's easy to see everything that's happening but at the same time not see the 
bits you don't really care about.

Mark



On Sunday 18 February 2007 5:41 pm, Francesc Romà i Frigolé wrote:
> Hello,
>
> I want to create a web application which is basically CRUD, and I was
> wondering if there is a scaffolding script that can do it for catalyst. It
> has to support tables with composite primary keys.
>
> I thought InstantCRUD would be a good solution but i quickly discovered
> that it doesn't handle composite primary keys ( it actually says it on the
> documentation ). I naively thought that it wouldn't take much time to add
> such functionality, and I started preparing a patch for it. I actually
> managed it to work for Edit/Update and Delete (I think so), but I got stuck
> with the Create part. The problem is, at least, that InstantCRUD depends on
> DBSchema.pm which has the same limitation.
>
> Now I got the feeling I'm wasting my time because somebody must already
> have a solution for this problem, right?
>
> BTW: I've seen a lot of talk about Reaction on the list recently but I
> haven't quite got what is it about, and apparently is undocumented. Does it
> have any relation with CRUD?
>
> thanks in advance,
> Francesc



More information about the Catalyst mailing list