=head2 SELECT DISTINCT with multiple columns
- my $rs = $schema->resultset('Foo')->search(
+ my $rs = $schema->resultset('Artist')->search(
{},
{
- select => [
- { distinct => [ $source->columns ] }
- ],
- as => [ $source->columns ] # remember 'as' is not the same as SQL AS :-)
+ columns => [ qw/artistid name rank/ ],
+ distinct => 1
+ }
+ );
+
+ my $rs = $schema->resultset('Artist')->search(
+ {},
+ {
+ columns => [ qw/artistid name rank/ ],
+ group_by => [ qw/artistid name rank/ ],
}
);
+ # Equivalent SQL:
+ # SELECT me.artistid, me.name, me.rank
+ # FROM artist me
+ # GROUP BY artistid, name, rank
+
=head2 SELECT COUNT(DISTINCT colname)
- my $rs = $schema->resultset('Foo')->search(
+ my $rs = $schema->resultset('Artist')->search(
{},
{
- select => [
- { count => { distinct => 'colname' } }
- ],
- as => [ 'count' ]
+ columns => [ qw/name/ ],
+ distinct => 1
}
);
- my $count = $rs->next->get_column('count');
+ my $rs = $schema->resultset('Artist')->search(
+ {},
+ {
+ columns => [ qw/name/ ],
+ group_by => [ qw/name/ ],
+ }
+ );
+
+ my $count = $rs->count;
+
+ # Equivalent SQL:
+ # SELECT COUNT( * ) FROM (SELECT me.name FROM artist me GROUP BY me.name) count_subq:
=head2 Grouping results
# do whatever else you wanted if it was a new row
}
+=head2 Static sub-classing DBIx::Class result classes
+
+AKA adding additional relationships/methods/etc. to a model for a
+specific usage of the (shared) model.
+
+B<Schema definition>
+
+ package My::App::Schema;
+
+ use base DBIx::Class::Schema;
+
+ # load subclassed classes from My::App::Schema::Result/ResultSet
+ __PACKAGE__->load_namespaces;
+
+ # load classes from shared model
+ load_classes({
+ 'My::Shared::Model::Result' => [qw/
+ Foo
+ Bar
+ /]});
+
+ 1;
+
+B<Result-Subclass definition>
+
+ package My::App::Schema::Result::Baz;
+
+ use strict;
+ use warnings;
+ use base My::Shared::Model::Result::Baz;
+
+ # WARNING: Make sure you call table() again in your subclass,
+ # otherwise DBIx::Class::ResultSourceProxy::Table will not be called
+ # and the class name is not correctly registered as a source
+ __PACKAGE__->table('baz');
+
+ sub additional_method {
+ return "I'm an additional method only needed by this app";
+ }
+
+ 1;
+
=head2 Dynamic Sub-classing DBIx::Class proxy classes
AKA multi-class object inflation from one table
use base qw/DBIx::Class::Schema/;
- __PACKAGE__->load_namespaces;
+ __PACKAGE__->load_namespaces;
+
+ 1;
B<Proxy-Class definitions>
print "I am a regular user.\n";
return ;
}
-
+
+ 1;
+
package My::Schema::Result::User::Admin;
{
print "I am doing admin stuff\n";
return ;
- }
+ }
+
+ 1;
B<Test File> test.pl