dbix::class::helper::resultset::virtualview(3pm) [debian man page]
DBIx::Class::Helper::ResultSet::VirtualView(3pm) User Contributed Perl Documentation DBIx::Class::Helper::ResultSet::VirtualView(3pm)NAME
DBIx::Class::Helper::ResultSet::VirtualView - Clean up your SQL namespace (DEPRECATED)
VERSION
version 2.013002
SYNOPSIS
# note that this is normally a component for a ResultSet
package MySchema::ResultSet::Bar;
use strict;
use warnings;
use parent 'DBIx::Class::ResultSet';
__PACKAGE__->load_components('Helper::ResultSet::VirtualView');
# and then in code that uses the ResultSet Join with relation x
my $rs = $schema->resultset('Bar')->search({'x.name' => 'abc'},{ join => 'x' });
# 'x' now pollutes the query namespace
# So the following works as expected
my $ok_rs = $rs->search({'x.other' => 1});
# But this doesn't: instead of finding a 'Bar' related to two x rows (abc and
# def) we look for one row with contradictory terms and join in another table
# (aliased 'x_2') which we never use
my $broken_rs = $rs->search({'x.name' => 'def'});
my $rs2 = $rs->as_virtual_view;
# doesn't work - 'x' is no longer accessible in $rs2, having been sealed away
my $not_joined_rs = $rs2->search({'x.other' => 1});
# works as expected: finds a 'table' row related to two x rows (abc and def)
my $correctly_joined_rs = $rs2->search({'x.name' => 'def'});
DESCRIPTION
This component is will allow you to clean up your SQL namespace. See "NOTE" in DBIx::Class::Helper::ResultSet for a nice way to apply it
to your entire schema.
DEPRECATED
This component has been suplanted by DBIx::Class::ResultSet::as_subselect_rs. In the next major version(3) we will begin issuing a
warning on it's use. In the major version after that(4) we will remove it entirely.
METHODS
as_virtual_view
Act as a barrier to SQL symbols. The resultset provided will be made into a "virtual view" by including it as a subquery within the from
clause. From this point on, any joined tables are inaccessible to ->search on the resultset (as if it were simply where-filtered without
joins). See "SYNOPSIS" for example.
NOTE
You don't have to use this as a Component. If you prefer you can use it in the following manner:
# in code using ResultSet:
use DBIx::Class:Helper::VirtualView;
my $new_rs = DBIx::Class::Helper::VirtualView::as_virtual_view($rs);
THANKS
Thanks to ijw from #dbix-class for the idea for this helper (originally called seal), most of the code, and most of the documentation.
AUTHOR
Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Arthur Axel "fREW" Schmidt.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
perl v5.14.2 2012-06-18 DBIx::Class::Helper::ResultSet::VirtualView(3pm)
Check Out this Related Man Page
DBIx::Class::Helper::ResultSet::CorrelateRelationship(3pUser Contributed Perl DocumentatDBIx::Class::Helper::ResultSet::CorrelateRelationship(3pm)NAME
DBIx::Class::Helper::ResultSet::CorrelateRelationship - Easily correlate your ResultSets
VERSION
version 2.013002
SYNOPSIS
package MyApp::Schema::ResultSet::Author;
use base 'DBIx::Class::ResultSet';
__PACKAGE__->load_components(qw(Helper::ResultSet::CorrelateRelationship));
sub with_book_count {
my $self = shift;
$self->search(undef, {
'+columns' => {
book_count => $self->correlate('book')->count_rs->as_query
}
});
}
1;
And then elsewhere, like in a controller:
my $rows = $schema->resultset('Author')->with_book_count->all;
DESCRIPTION
Correlated queries are one of the coolest things I've learned about for SQL since my initial learning of SQL. Unfortunately they are
somewhat confusing. DBIx::Class has supported doing them for a long time, but generally people don't think of them because they are so
rare. I won't go through all the details of how they work and cool things you can do with them, but here are a couple high level things
you can use them for to save you time or effort.
If you want to select a list of authors and counts of books for each author, you could use "group_by" and something like "COUNT(book.id)",
but then you'd need to make your select list match your "group_by" and it would just be a hassle forever after that. The "SYNOPSIS" is a
perfect example of how to implement this.
If you want to select a list of authors and two separate kinds of counts of books for each author, as far as I know, you must use a
correlated subquery in DBIx::Class. Here is an example of how you might do that:
package MyApp::Schema::ResultSet::Author;
use base 'DBIx::Class::ResultSet';
__PACKAGE__->load_components(qw(Helper::ResultSet::CorrelateRelationship));
sub with_good_book_count {
my $self = shift;
$self->search(undef, {
'+columns' => {
good_book_count => $self->correlate('books')->good->count_rs->as_query
}
});
}
sub with_bad_book_count {
my $self = shift;
$self->search(undef, {
'+columns' => {
bad_book_count => $self->correlate('books')->bad->count_rs->as_query
}
});
}
1;
And then elsewhere, like in a controller:
my $rows = $schema->resultset('Author')
->with_bad_book_count
->with_good_book_count
->all;
This assumes that the Book resultset has "good" and "bad" methods.
See "NOTE" in DBIx::Class::Helper::ResultSet for a nice way to apply it to your entire schema.
METHODS
correlate
$rs->correlate($relationship_name)
Correlate takes a single argument, a relationship for the invocant, and returns a resultset that can be used in the selector list.
AUTHOR
Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Arthur Axel "fREW" Schmidt.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
perl v5.14.2 2012-06-18 DBIx::Class::Helper::ResultSet::CorrelateRelationship(3pm)