[Dbix-class] order_by question

africroissance africroissance at gmail.com
Thu Jan 20 09:28:25 GMT 2011


On Wed, Jan 19, 2011 at 9:26 PM, Rob Kinyon <rob.kinyon at gmail.com> wrote:

> What happened? Why do you think that doesn't work? Please provide what
> should have happened, what did happen, and why you're not happy with
> the outcome.
>
>

Thanks Rob,

Working with the example in
http://search.cpan.org/~abraxxa/DBIx-Class-0.08127/lib/DBIx/Class/Manual/Ex=
ample.podI
wrote a method:

sub get_cds_ordered_by_number_of_tracks {
  print "CDS ordered by the number of tracks they have\n";
  my $rs =3D $schema->resultset('Cd')->search(
    {},
    {
      order_by =3D> \'tracks.count DESC',
      join     =3D> [qw/ tracks /],
    }
  );
  while (my $cd =3D $rs->next) {
    print $cd->title . "\n";
  }
  print "\n";
}

expecting that I would get:

CDS ordered by the number of tracks they have
Bad
Thriller
The Marshall Mathers LP

ie. A list of CDs ordered by the number of tracks they each have (Bad; 3,
Thriller; 2, The Marshall Mathers LP; 2)

However what I get is this:
CDS ordered by the number of tracks they have
DBIx::Class::ResultSet::next(): DBI Exception: DBD::SQLite::db
prepare_cached failed: no such column: tracks.count [for Statement "SELECT
me.cdid, me.artist, me.title FROM cd me LEFT JOIN track tracks ON
tracks.cd=3D me.cdid ORDER BY tracks.count DESC"] at
testdb.pl line 135


which makes sense, however I am not sure what the right syntax is to get
"parents ordered by the number of children they have"..

Thanks again everyone.







> Rob
>
> On Wed, Jan 19, 2011 at 13:28, africroissance <africroissance at gmail.com>
> wrote:
> > Hello,
> > I am very very new to using DBIx::Class -- please do forgive the naivet=
=E9
> of
> > the question - I have a question about using the order_by attribute.
> >
> > Given the simple example in the CPAN docs:
> >
> http://search.cpan.org/~danieltwc/DBIx-Class-0.07002/lib/DBIx/Class/Manua=
l/Example.pod<http://search.cpan.org/%7Edanieltwc/DBIx-Class-0.07002/lib/DB=
Ix/Class/Manual/Example.pod>
> >
> > What would be the right syntax to get back a list of CDs ordered by the
> > number of tracks each has? I thought something like the following would
> work
> > but I was wrong:
> >
> > sub get_cds_ordered_by_number_of_tracks {
> >   print "CDS ordered by the number of tracks they have\n";
> >   my $rs =3D $schema->resultset('Cd')->search(
> >     {},
> >     {
> >       order_by =3D> \'tracks.count DESC',
> >       join     =3D> [qw/ tracks /],
> >     }
> >   );
> >   while (my $cd =3D $rs->next) {
> >     print $cd->title . "\n";
> >   }
> >   print "\n";
> > }
> >
> > Thank you very much.
> > _______________________________________________
> > 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@lists.scsys.co.uk
> >
>
>
>
> --
> 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@lists.scsys.co.uk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20110120/022=
b581b/attachment.htm


More information about the DBIx-Class mailing list