Counting one to many

4 messages Options
Embed this post
Permalink
Dan Horne

Counting one to many

Reply Threaded More More options
Print post
Permalink
Hi

Say I have two tables, invoices and invoice_lines, where the  
relationship is one to many. If we pass a resultset of the invoices to  
a TT template, is it possible to chain an a method on the resultset to  
get a count of he invoice lines? I don't mind going back to the db to  
do exta fetches per line as there will never be more than 20 invoices.  
i.e., I want to do something like this:

         [% WHILE (invoice = invoices.next) %]
         <tr>
             <td>[% invoice.invoice_number %]</td>
             <td>[% invoice.invoice_lines.count %]</td> <-- something  
that does this
         </tr>
         [% END %]

Regards

Dan


_______________________________________________
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@...
Rob Kinyon

Re: Counting one to many

Reply Threaded More More options
Print post
Permalink
invoice.invoice_lines_rs.count

Rob

On Mon, Jun 29, 2009 at 17:48, Dan Horne<[hidden email]> wrote:

> Hi
>
> Say I have two tables, invoices and invoice_lines, where the relationship is
> one to many. If we pass a resultset of the invoices to a TT template, is it
> possible to chain an a method on the resultset to get a count of he invoice
> lines? I don't mind going back to the db to do exta fetches per line as
> there will never be more than 20 invoices. i.e., I want to do something like
> this:
>
>        [% WHILE (invoice = invoices.next) %]
>        <tr>
>            <td>[% invoice.invoice_number %]</td>
>            <td>[% invoice.invoice_lines.count %]</td> <-- something that
> does this
>        </tr>
>        [% END %]
>
> Regards
>
> Dan
>
>
> _______________________________________________
> 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@...
>



--
Thanks,
Rob Kinyon

_______________________________________________
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@...
Dan Horne

Re: Counting one to many

Reply Threaded More More options
Print post
Permalink
Brilliant - exactly what I was after


On 30/06/2009, at 09:52 AM, Rob Kinyon wrote:

> invoice.invoice_lines_rs.count
>
> Rob
>
> On Mon, Jun 29, 2009 at 17:48, Dan Horne<[hidden email]>  
> wrote:
>> Hi
>>
>> Say I have two tables, invoices and invoice_lines, where the  
>> relationship is
>> one to many. If we pass a resultset of the invoices to a TT  
>> template, is it
>> possible to chain an a method on the resultset to get a count of he  
>> invoice
>> lines? I don't mind going back to the db to do exta fetches per  
>> line as
>> there will never be more than 20 invoices. i.e., I want to do  
>> something like
>> this:
>>
>>        [% WHILE (invoice = invoices.next) %]
>>        <tr>
>>            <td>[% invoice.invoice_number %]</td>
>>            <td>[% invoice.invoice_lines.count %]</td> <-- something  
>> that
>> does this
>>        </tr>
>>        [% END %]
>>
>> Regards
>>
>> Dan
>>
>>
>> _______________________________________________
>> 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@...
>>
>
>
>
> --
> Thanks,
> Rob Kinyon
>
> _______________________________________________
> 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@...
Rob Kinyon

Re: Counting one to many

Reply Threaded More More options
Print post
Permalink
The reason for this is because TT provides an implicit list context to
all methods. This is why the _rs methods exist. It should be your
first stab when solving a problem in TT.

2009/6/29 Dan Horne <[hidden email]>:

> Brilliant - exactly what I was after
>
>
> On 30/06/2009, at 09:52 AM, Rob Kinyon wrote:
>
>> invoice.invoice_lines_rs.count
>>
>> Rob
>>
>> On Mon, Jun 29, 2009 at 17:48, Dan Horne<[hidden email]> wrote:
>>>
>>> Hi
>>>
>>> Say I have two tables, invoices and invoice_lines, where the relationship is
>>> one to many. If we pass a resultset of the invoices to a TT template, is it
>>> possible to chain an a method on the resultset to get a count of he invoice
>>> lines? I don't mind going back to the db to do exta fetches per line as
>>> there will never be more than 20 invoices. i.e., I want to do something like
>>> this:
>>>
>>>       [% WHILE (invoice = invoices.next) %]
>>>       <tr>
>>>           <td>[% invoice.invoice_number %]</td>
>>>           <td>[% invoice.invoice_lines.count %]</td> <-- something that
>>> does this
>>>       </tr>
>>>       [% END %]
>>>
>>> Regards
>>>
>>> Dan
>>>
>>>
>>> _______________________________________________
>>> 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@...
>>>
>>
>>
>>
>> --
>> Thanks,
>> Rob Kinyon
>>
>> _______________________________________________
>> 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@...
>



--
Thanks,
Rob Kinyon

_______________________________________________
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@...