PyrexKidd has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks.
I am working on a CRUD application using the Catalyst Framework and a Sqlite DB with a DBIx interface.
I have a controller with a list function like so:
sub list :Local { # Retrieve the usual Perl OO '$self' for this object. $c is the Ca +talyst # 'Context' that's used to 'glue together' the various components # that make up the application my ($self, $c) = @_; # Retrieve all of the book records as book model objects and store + in the # stash where they can be accessed by the TT template #$c->stash(resultset => [$c->model('DB::User')->all]); $c->stash (resultset => $c->model('DB::User')->all); use Data::Dumper; my $aref = $c->model('User'); $c->log->debug( Dumper $aref ); # Set the TT template to use. You will almost always want to do t +his # in your action methods (action methods respond to user input in # your controllers). $c->stash(template => 'main/list.tt'); }
Then I am using the following template:
[% cols = resultset.result_source.columns; FOREACH x IN cols; "<li>" +_ resultset.$x _ "</li>"; END %] [% # FOREACH row IN resultset %]Username: [% #row.username %]<br />[% +#END %] <table> <tr> <th>User Name</th> <th>Password</th> <th>Email</th> <th>FName</th> <th>LName</th> <th>Active</th> </tr> [% FOREACH row IN resultset %] <tr> <td>[% row.username %]</td> <td>[% row.password %]</td> <td>[% row.email_address %]</td> <td>[% row.first_name %]</td> <td>[% row.last_name %]</td> <td>[% row.active %]</td> </td> [% END %] </table>
the first line works, when I call:
$c->stash (resultset => $c->model('DB::User')->all);
but it only displays the first record. My table also only displays the first record in the table.
the first line doesn't work, and m y table displays all records when I call:
$c->stash (resultset => [$c->model('DB::User')->all]);
The problem is, what if I want to display the results of a different DB?
Seems that I should be able to use the same template... maybe I'm just not groking the model.
The tutorial says these are the same, the first two return undefined values:
# the long way my $rs = $c->model('FilmDB')->schema->resultset('Actor'); # using the shortcut method on the model object my $rs = $c->model('FilmDB')->resultset('Actor'); # using the generated class directly my $rs = $c->model('FilmDB::Actor');
Also, this returns a "can't find method" error:
my $dbic = $c->model('FilmDB')->schema; my $dbic = $c->model('DB::FilmDB')->schema;
I promise I have read the tutorials more than once.
I think the issue is probably something simple that I'm just misunderstanding.
Can someone please help me instead of just pointing me to the manuals?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Catalyst DBIx Help
by Anonymous Monk on Aug 12, 2011 at 09:10 UTC | |
|
Re: Catalyst DBIx Help
by Solo (Deacon) on Aug 25, 2011 at 08:40 UTC |