1 package DBIx::Class::ResultSource::View;
6 use base 'DBIx::Class::ResultSource';
8 __PACKAGE__->mk_group_accessors(
9 'simple' => qw(is_virtual view_definition deploy_depends_on) );
13 DBIx::Class::ResultSource::View - ResultSource object representing a view
17 package MyApp::Schema::Result::Year2000CDs;
19 use base qw/DBIx::Class::Core/;
21 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
23 __PACKAGE__->table('year2000cds');
24 __PACKAGE__->result_source_instance->is_virtual(1);
25 __PACKAGE__->result_source_instance->view_definition(
26 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
28 __PACKAGE__->add_columns(
30 data_type => 'integer',
31 is_auto_increment => 1,
34 data_type => 'integer',
37 data_type => 'varchar',
44 View object that inherits from L<DBIx::Class::ResultSource>
46 This class extends ResultSource to add basic view support.
48 A view has a L</view_definition>, which contains a SQL query. The query can
49 only have parameters if L</is_virtual> is set to true. It may contain JOINs,
50 sub selects and any other SQL your database supports.
52 View definition SQL is deployed to your database on
53 L<DBIx::Class::Schema/deploy> unless you set L</is_virtual> to true.
55 Deploying the view does B<not> translate it between different database
56 syntaxes, so be careful what you write in your view SQL.
58 Virtual views (L</is_virtual> true), are assumed to not
59 exist in your database as a real view. The L</view_definition> in this
60 case replaces the view name in a FROM clause in a subselect.
64 Having created the MyApp::Schema::Year2000CDs schema as shown in the SYNOPSIS
67 $y2000_cds = $schema->resultset('Year2000CDs')
70 $count = $schema->resultset('Year2000CDs')
74 If you modified the schema to include a placeholder
76 __PACKAGE__->result_source_instance->view_definition(
77 "SELECT cdid, artist, title FROM cd WHERE year = ?"
80 and ensuring you have is_virtual set to true:
82 __PACKAGE__->result_source_instance->is_virtual(1);
86 $y2001_cds = $schema->resultset('Year2000CDs')
87 ->search({}, { bind => [2001] })
89 $count = $schema->resultset('Year2000CDs')
90 ->search({}, { bind => [2001] })
97 =item is_virtual set to false
99 $schema->resultset('Year2000CDs')->all();
101 SELECT cdid, artist, title FROM year2000cds me
103 =item is_virtual set to true
105 $schema->resultset('Year2000CDs')->all();
107 SELECT cdid, artist, title FROM
108 (SELECT cdid, artist, title FROM cd WHERE year ='2000') me
116 __PACKAGE__->result_source_instance->is_virtual(1);
118 Set to true for a virtual view, false or unset for a real
121 =head2 view_definition
123 __PACKAGE__->result_source_instance->view_definition(
124 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
127 An SQL query for your view. Will not be translated across database
130 =head2 deploy_depends_on
132 __PACKAGE__->result_source_instance->deploy_depends_on(
133 ["MyApp::Schema::Result::Year","MyApp::Schema::Result::CD"]
136 Specify the views (and only the views) that this view depends on.
137 Pass this an array reference of fully qualified result classes.
139 =head1 OVERRIDDEN METHODS
143 Returns the FROM entry for the table (i.e. the view name)
144 or the SQL as a subselect if this is a virtual view.
149 $_[0]->throw_exception('from() is not a setter method') if @_ > 1;
151 ? \( '(' . $_[0]->view_definition .')' )
165 my ( $self, @args ) = @_;
166 my $new = $self->next::method(@args);
167 $new->{deploy_depends_on} =
169 @{ $new->{deploy_depends_on} || [] } }
170 unless ref $new->{deploy_depends_on} eq 'HASH';
174 =head1 FURTHER QUESTIONS?
176 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
178 =head1 COPYRIGHT AND LICENSE
180 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
181 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
182 redistribute it and/or modify it under the same terms as the
183 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.