in reply to Problems with DBIx::Class and SQL JOIN's

When you get this running you will have two URIs to play with.

http://localhost:3000/walk_object/schema to see a table of your schema. There is also a nice module one of the awesome DBIC kids put together to do this stuff and more -- DBICx::AutoDoc.

And you have http://localhost:3000/walk_object/ResultClassName/_id_. So you should be able to use it like-

http://localhost:3000/walk_object/Role/1 and http://localhost:3000/walk_object/User/3 etc. I did not check the layout on anything but Firefox so the CSS off the top of my head might be wonky.

A Catalyst controller stub to play with

The schema thing isn't the best style... You get the idea. It requires no template. walk_object does require a template. (I updated the model namespace to match what I think yours is.)

package MyApp::Controller::Play; use strict; use warnings; use parent 'Catalyst::Controller'; use YAML (); use CGI ":standard"; my $th_style = { -style => "font-family:sans-serif; font-size:13px; text-align:righ +t; border:1px solid black; padding: 1px 3px; margin:1px;background-co +lor:#fef;" }; my $td_style = { -style => "font-family:sans-serif; font-size:12px;border:1px solid + black; padding: 1px 3px; margin:1px; background-color:#efe" }; my $td_style2 = { -style => "font-family:sans-serif; font-size:12px;border:1px solid + black; padding: 1px 3px; margin:1px; background-color:#eff" }; sub schema : Local { my ( $self, $c ) = @_; my $schema = $c->model("DB")->schema; my @sources = start_html("DB schema"); for my $src ( sort { "$a" cmp "$b" } $schema->sources ) { push @sources, h2($src); push @sources, '<table style="width:90%; border:2px solid gray +; background-color:#ddd">'; for my $col ( $schema->source($src)->columns ) { push @sources, "<tr>"; push @sources, th($th_style, $col); my $col_info = $schema->source($src)->column_info($col); for my $key ( reverse sort keys %{ $col_info } ) { push @sources, $key ? td($td_style, $key) : td(" "); my $info = ref $col_info->{$key} ? YAML::Dump($col_info->{$key}) : $col_info->{$key}; push @sources, $info ? td($td_style2, $info) : td(" "); } push @sources, "</tr>"; } push @sources, "</table>"; } $c->response->body( join("\n",@sources) ); } sub walk_object : Local Args(2) { my ( $self, $c, $type, $id ) = @_; my $object = $c->model("DB::$type")->find($id) or die "No can do, no $type with id '$id'"; $c->stash( object => $object ); } 1;

The view

I have moved from Template to Template::Alloy for the last project or two. You might have to install it to play with this. I think you could do the blessed jazz in TT2 but I don't know the syntax off the top of my head.

package MyApp::View::Alloy; use strict; no warnings "uninitialized"; use parent "Catalyst::View::TT::Alloy"; use Scalar::Util "blessed"; __PACKAGE__->config( RECURSION => 1 ); Template::Alloy->define_vmethod ( "SCALAR", blessed => sub { blessed(+shift); } );

The template: walk_object.tt

This is Alloy but I think it's all valid for TT2 as well.

[% INCLUDE show_object title = object.blessed() obj = object %] [% BLOCK show_object %] <div style="clear: both; float:left; padding: 1ex; width: 100%"> <h3 style="margin:0; padding:0;">[% title %]</h3> [% FOR col IN obj.columns %] <b style="width: 25%; text-align: right; display:block; float:left +; margin-right: 1ex"> [% col %] </b> <div style="width: 70%; float: left;"> [% IF obj.$col.blessed %] [% INCLUDE show_object title = obj.$col.blessed() obj = obj.$col %] [% ELSE %] [% obj.$col || "<i>na</i>" %] [% END %] </div> [% END %] </div> [% END %]

The template might be a little tricky to read, but give it try. You can see how it's possible to iterate on columns and descend into relationships (not many_to_many yet, sad to say but maybe in the next major release).

Good luck and let me know if that won't fly for you. I tested it but in a different namespace so I hope I didn't introduce any bugs setting it up for posting here.

Replies are listed 'Best First'.
Re: Sample schema and object introspection with DBIx::Class and Catalyst
by Tommy (Chaplain) on Jun 12, 2009 at 12:35 UTC

    No, the template's not tricky to read. I want to thank you for the time you spent putting that example together. That will become very useful as I use DBIx::Class and Catalyst more and more.

    Thanks again!

    --
    Tommy