[Catalyst] DBIx::Class newbish count problems
Will Hawes
info at whawes.co.uk
Thu Jan 19 10:00:21 CET 2006
Dave C wrote:
> I'm attempting to add a GUI using Catalyst to an existing (crappy) DB
> structure. Because of this, I had to disable DBIx::Class::Loader
> relationships. I have 2 tables that I'm attempting to set up
> relationships between manually:
>
> package VhostStats::Model::DBIC::Vhosts;
>
> use strict;
> use warnings;
> use base 'DBIx::Class::Core';
>
> __PACKAGE__->table('vhosts');
> __PACKAGE__->add_columns(qw/vid hostname/);
> __PACKAGE__->set_primary_key('vid');
> __PACKAGE__->has_many(users => 'VhostStats::Model::DBIC::Users', 'vid');
>
> 1;
>
> package VhostStats::Model::DBIC::Users;
>
> use strict;
> use warnings;
> use base 'DBIx::Class::Core';
>
> __PACKAGE__->table('users');
> __PACKAGE__->add_columns(qw/uid vid username/);
> __PACKAGE__->set_primary_key('uid');
> __PACKAGE__->belongs_to('vid' => 'VhostStats::Model::DBIC::Vhosts');
>
> __PACKAGE__->has_one(vid => 'VhostStats::Model::DBIC::Vhosts');
>
> 1;
>
> My problem comes when I attempt to get the total number of users on each
> given Vhost. I'm trying to do something such as:
>
> [% FOR vhost IN vhosts %]
> <tr><td>vhost.hostname</td><td> vhost.users.count</td></tr>
> [% END %]
>
> However, vhost.users successfully returns all rows but vhost.users.count
> returns nothing. If vhost.count works, shouldn't vhost.users.count by
> following the relationship? Currently, I'm just looping through
> vhost.users manually but that seems too hackish.
>
> Any help would be appreciated.
>
> dave.
1) What version of DBIx::Class are you using?
2) Why do you have both belongs_to and has_one relationships defined
from Users to Vhosts? Try removing the has_one and see if that makes any
difference. If not, what does dumping vhost.users give you? E.g.:
[% USE Dumper; Dumper.dump(vhost.users) %]
Incidentally, it can be also helpful to run problem code outside of TT
while you debug it. IME warnings/errors that can help determine a
problem do not always make it to your template's output.
More information about the Catalyst
mailing list