[Dbix-class] Unable to chain resultset method on a many_to_many relationship method

Anthony Gladdish a.j.gladdish at newcastle.ac.uk
Mon Oct 26 16:19:55 GMT 2009


Hi all,

I have the following tables and relationships:

Module --< ModuleTag >-- Tag

MyApp::Result::Module:

__PACKAGE__->has_many( 'module_tags' => ' MyApp::Result::ModuleTag', 'module' );
__PACKAGE__->many_to_many('tags' => 'module_tags', 'tag' );

MyApp::Result::Tag:

__PACKAGE__->has_many( 'tag_modules' => 'MyApp::Result::ModuleTag', 'tag' );
__PACKAGE__->many_to_many('modules' => 'tag_modules', 'module' );

MyApp::Result::ModuleTag:

__PACKAGE__->belongs_to( tag => ' MyApp::Result::Tag', 'tag' );
__PACKAGE__->belongs_to( module => ' MyApp::Result::Module', 'module' );

MyApp::ResultSet::Tag:

sub weightings {
    my $self = shift;
    return $self->search_rs(
		{},
		{ 
			join     => 'tag_modules',
	  	      select   => [ 'me.id', 'me.name', { count => 'tag_modules.tag' } ],
			as       => [qw/ id name weighting  /],
			group_by => 'me.name',
		}
	);
}

Test cases:

# 1:
$schema->resultset('Tag')->weightings();

... this does what I expect and returns correct results and following sql:
# SELECT me.id, me.name, COUNT( tag_modules.tag ) 
# FROM Tag me LEFT JOIN Module_Tag tag_modules ON tag_modules.tag = me.id 
# GROUP BY me.name

# 2:
$module1->tags->weightings();

... this dies producing error:

no such column: me.name [for Statement "SELECT me.id, me.name, COUNT( tag_modules.tag ) FROM Module_Tag me JOIN Tag tag ON tag.id = me.tag LEFT JOIN Module_Tag tag_modules ON tag_modules.tag = tag.id WHERE ( me.module = ? ) GROUP BY me.name"]

I was expecting this resultset chaining to work. I'm not sure if I'm missing a trick here in my aliases, or if this should be possible or if I'm doing it wrong.
Anybody have any suggestions?

Thanks,
Anthony



More information about the DBIx-Class mailing list