Unable to chain resultset method on a many_to_many relationship method

6 messages Options
Embed this post
Permalink
Anthony Gladdish

Unable to chain resultset method on a many_to_many relationship method

Reply Threaded More More options
Print post
Permalink
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

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Michael Blanco

Re: Unable to chain resultset method on a many_to_many relationship method

Reply Threaded More More options
Print post
Permalink
Try using the following

current_source_alias

http://search.cpan.org/~ribasushi/DBIx-Class-0.08112/lib/DBIx/Class/ResultSet.pm

----------------------------
Michael Blanco




On Mon, Oct 26, 2009 at 12:19 PM, Anthony Gladdish <[hidden email]> wrote:
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

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Anthony Gladdish

RE: Unable to chain resultset method on a many_to_many relationship method

Reply Threaded More More options
Print post
Permalink
In reply to this post by Anthony Gladdish
Hi,

>From: Michael Blanco [mailto:[hidden email]]
>Sent: 26 October 2009 21:36
>To: DBIx::Class user and developer list
>Subject: Re: [Dbix-class] Unable to chain resultset method on a many_to_many relationship method
>
>Try using the following
>
>current_source_alias
>
>http://search.cpan.org/~ribasushi/DBIx-Class-0.08112/lib/DBIx/Class/ResultSet.pm
>
>----------------------------
>Michael Blanco
Thanks Michael, this worked.

I've attached a document patch. What's the correct procedure to apply this?

Anthony


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...

Cookbook.pod.diff (830 bytes) Download Attachment
Michael Blanco

Re: Unable to chain resultset method on a many_to_many relationship method

Reply Threaded More More options
Print post
Permalink
I have used it as follows

my $current_source_alias = $self->current_source_alias;

    my $conditions = {
        "$current_source_alias.status_id" => { '-in' => [ 3, 11, 12, 15, 17, 18, 23 ] },
...

----------------------------
Michael Blanco




On Tue, Oct 27, 2009 at 6:30 AM, Anthony Gladdish <[hidden email]> wrote:
Hi,

>From: Michael Blanco [mailto:[hidden email]]
>Sent: 26 October 2009 21:36
>To: DBIx::Class user and developer list
>Subject: Re: [Dbix-class] Unable to chain resultset method on a many_to_many relationship method
>
>Try using the following
>
>current_source_alias
>
>http://search.cpan.org/~ribasushi/DBIx-Class-0.08112/lib/DBIx/Class/ResultSet.pm
>
>----------------------------
>Michael Blanco

Thanks Michael, this worked.

I've attached a document patch. What's the correct procedure to apply this?

Anthony

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Peter Rabbitson-2

Re: Unable to chain resultset method on a many_to_many relationship method

Reply Threaded More More options
Print post
Permalink
In reply to this post by Anthony Gladdish
Anthony Gladdish wrote:

> Hi,
>
>> From: Michael Blanco [mailto:[hidden email]]
>> Sent: 26 October 2009 21:36
>> To: DBIx::Class user and developer list
>> Subject: Re: [Dbix-class] Unable to chain resultset method on a many_to_many relationship method
>>
>> Try using the following
>>
>> current_source_alias
>>
>> http://search.cpan.org/~ribasushi/DBIx-Class-0.08112/lib/DBIx/Class/ResultSet.pm
>>
>> ----------------------------
>> Michael Blanco
>
> Thanks Michael, this worked.
>
> I've attached a document patch. What's the correct procedure to apply this?
>
> Anthony
>

Can you add an actual code example too?

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Anthony Gladdish

RE: Unable to chain resultset method on a many_to_many relationship method

Reply Threaded More More options
Print post
Permalink
>>> From: Michael Blanco [mailto:[hidden email]]

>>> Sent: 26 October 2009 21:36
>>> To: DBIx::Class user and developer list
>>> Subject: Re: [Dbix-class] Unable to chain resultset method on a
>many_to_many relationship method
>>>
>>> Try using the following
>>>
>>> current_source_alias
>>>
>>> http://search.cpan.org/~ribasushi/DBIx-Class-
>0.08112/lib/DBIx/Class/ResultSet.pm
>>>
>>> ----------------------------
>>> Michael Blanco
>>
>> Thanks Michael, this worked.
>>
>> I've attached a document patch. What's the correct procedure to apply
>this?
>>
>> Anthony
>>
>
>Can you add an actual code example too?
Patch attached. Let me know if you need further changes.

Thanks,
Anthony


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...

Cookbook.pod.patch (1K) Download Attachment