[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] Catalyst - database access syntax



For the programmers out there...

Ryan's talk covered database syntax in ColdFusion, which made me
regret that I didn't demo that too. Here's the default database
manipulation syntax in Catalyst + Template Toolkit, if you're
interested.

To add a list of Twitter accounts to the little app I demo'd you can
throw this into your HTML (the tags are Template Toolkit syntax):

------------------
<table border=1>
<tr><th>Twitter ID</th><th>Name</th></tr>
[% FOREACH followers IN
c.model('BarCampOmaha::Model::DB::Followers').all %]
   <tr>
   <td>[% followers.twitter_id %]</td>
   <td>[% followers.name %]</td>
   <td><a href="/twitter/delete?twitter_id=[% followers.twitter_id
%]">delete</a></td>
   </tr>
[% END %]
</table>

<form action="/twitter/add">
<input type="text" name="twitter_id">
<input type="text" name="name">
<input type="submit" value="Add me">
</form>
------------------

This reads out of a little sqlite database on my laptop, but could be
any data source. Now we need two new Actions: a delete and an add.

So we throw this into Controller/Twitter

------------
sub add : Path("add") {
    my ( $self, $c ) = @_;

    $c->model('BarCampOmaha::Model::DB::Followers')->create({
       twitter_id => $c->req->param('twitter_id'),
       name       => $c->req->param('name'),
    });
    $c->res->redirect('/barcampomaha/homepage');
}

sub delete : Path("delete") {
    my ( $self, $c ) = @_;

    $c->model('BarCampOmaha::Model::DB::Followers')->search({
       twitter_id => $c->req->param('twitter_id'),
    })->delete_all;
    $c->res->redirect('/barcampomaha/homepage');
}
------------

All done.

Cheers,

j
Omaha Perl Mongers: http://omaha.pm.org