[Dbix-class] [Fwd: Re: [Catalyst] DBIx::Class newbish count problems]

Will Hawes info at whawes.co.uk
Wed Jan 25 10:28:06 CET 2006


Will Hawes wrote:
> Forwarded to list...
> 
> -------- Original Message --------
> Subject:     Re: [Catalyst] DBIx::Class newbish count problems
> Date:     Thu, 19 Jan 2006 17:12:36 -0500
> From:     Dave C <skinnydill at gmail.com>
> To:     info at whawes.co.uk
> References:
> <8924227e0601181616s2fd9d857l4af09325d999b93 at mail.gmail.com>
> <43CF5525.4090001 at whawes.co.uk>
> 
> 
> 
> On 1/19/06, *Will Hawes* <info at whawes.co.uk <mailto:info at whawes.co.uk>>
> wrote:
> 
> 
> 
>     1) What version of DBIx::Class are you using?
> 
> 
> 0.04001
> 
>     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.
> 
> 
> Oops.  That was left over from trying to see if there was any difference
> between     the two.  I removed the has_one, and there was no change.
> 
>     If not, what does dumping vhost.users give you? E.g.:
> 
>     [% USE Dumper; Dumper.dump (vhost.users) %]
> 
> 
> It gives me (as expected), an array of VhostStats::Model::Users objects
> for all users that match vid.  When I try and dump vhost.users.count,
> nothing is returned.
> 
>     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.
> 
> 
> Good suggestion.  I set up a simple test senario outside of Catalyst,
> and I'm able to list the number of matching rows with vhost.users.count
> successfully.  I tried to duplicate that exact setup, but am still
> having the same problems.
> 
> This works.  Even with only the has_many in MyApp::DBIC::Vhosts.
> 
> package MyApp::DBIC;
> 
> use strict;
> use base 'DBIx::Class';
> 
> __PACKAGE__->load_components(qw/PK::Auto::MySQL Core DB/);
> __PACKAGE__->connection('dbi:mysql:vhoststats;host= 127.0.0.1
> <http://127.0.0.1>', '', '');
> 
> 1;
> 
> package MyApp::DBIC::Users;
> 
> use strict;
> use warnings;
> use base 'MyApp::DBIC';
> 
> __PACKAGE__->table('users');
> __PACKAGE__->add_columns(qw/uid vid username aid/);
> __PACKAGE__->set_primary_key('uid');
> 
> 1;
> 
> package MyApp::DBIC::Vhosts;
> 
> use strict;
> use warnings;
> use base 'MyApp::DBIC';
> 
> __PACKAGE__->table('vhosts');
> __PACKAGE__->add_columns(qw/vid hostname/);
> __PACKAGE__->set_primary_key('vid');
> __PACKAGE__->has_many(users => 'MyApp::DBIC::Users', 'vid');
> 
> 1;
> 
> 
> And here's what I've tried to replicate in Catalyst.  The only major
> difference I saw was that I was inheriting from Catalyst::Model::DBIC
> originally.  I tried to change that to DBIx::Class and still no luck:
> 
> package VhostStats::Model::DBIC;
> 
> use strict;
> use base 'DBIx::Class';
> 
> __PACKAGE__->load_components(qw/PK::Auto::MySQL Core DB/);
> __PACKAGE__->connection('dbi:mysql:vhoststats;host= 127.0.0.1
> <http://127.0.0.1>', '', '');
> 
> 
> 1;
> 
> package VhostStats::Model::DBIC::Users;
> 
> use strict;
> use warnings;
> use base 'VhostStats::Model::DBIC';
> 
> __PACKAGE__->table('users');
> __PACKAGE__->add_columns(qw/uid vid username aid/);
> __PACKAGE__->set_primary_key('uid');
> 
> 1;
> 
> package VhostStats::Model::DBIC::Vhosts;
> 
> use strict;
> use warnings;
> use base 'VhostStats::Model::DBIC';
> 
> __PACKAGE__->table('vhosts');
> __PACKAGE__->add_columns(qw/vid hostname/);
> __PACKAGE__->set_primary_key('vid');
> __PACKAGE__->has_many(users => 'VhostStats::Model::DBIC::Users', 'vid');
> 
> 1;
> 
> 
> Any idears?  Thanks for your help so far!
> 
> dave.
> 

Dropping back on this because I'm not sure it got answered. I think this 
is the Template Toolkit problem encountered by several people recently.

Basically TT does things in list context, so in this case

[% vhost.users.count %]

is actually saying "get me the *array* vhost.users and call its count() 
method".

In a perl script on the other hand, things default to scalar context, so 
$vhost->users returns a DBIx::Class::ResultSet. This is why it works 
outside of TT.

There are mumblings on IRC about a workaround for this in future, but 
for now you can use TT's virtual array method "size" to obtain the 
number of users:

[% vhost.users.size %]



More information about the Dbix-class mailing list