1 package DBIx::Class::ResultSource::View;
6 use base 'DBIx::Class::ResultSource';
8 __PACKAGE__->mk_group_accessors( rsrc_instance_specific_attribute => qw(
9 is_virtual view_definition deploy_depends_on
14 DBIx::Class::ResultSource::View - ResultSource object representing a view
18 package MyApp::Schema::Result::Year2000CDs;
20 use base qw/DBIx::Class::Core/;
22 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
24 __PACKAGE__->table('year2000cds');
25 __PACKAGE__->result_source->is_virtual(1);
26 __PACKAGE__->result_source->view_definition(
27 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
29 __PACKAGE__->add_columns(
31 data_type => 'integer',
32 is_auto_increment => 1,
35 data_type => 'integer',
38 data_type => 'varchar',
45 View object that inherits from L<DBIx::Class::ResultSource>
47 This class extends ResultSource to add basic view support.
49 A view has a L</view_definition>, which contains a SQL query. The query can
50 only have parameters if L</is_virtual> is set to true. It may contain JOINs,
51 sub selects and any other SQL your database supports.
53 View definition SQL is deployed to your database on
54 L<DBIx::Class::Schema/deploy> unless you set L</is_virtual> to true.
56 Deploying the view does B<not> translate it between different database
57 syntaxes, so be careful what you write in your view SQL.
59 Virtual views (L</is_virtual> true), are assumed to not
60 exist in your database as a real view. The L</view_definition> in this
61 case replaces the view name in a FROM clause in a subselect.
65 Having created the MyApp::Schema::Year2000CDs schema as shown in the SYNOPSIS
68 $y2000_cds = $schema->resultset('Year2000CDs')
71 $count = $schema->resultset('Year2000CDs')
75 If you modified the schema to include a placeholder
77 __PACKAGE__->result_source->view_definition(
78 "SELECT cdid, artist, title FROM cd WHERE year = ?"
81 and ensuring you have is_virtual set to true:
83 __PACKAGE__->result_source->is_virtual(1);
87 $y2001_cds = $schema->resultset('Year2000CDs')
88 ->search({}, { bind => [2001] })
90 $count = $schema->resultset('Year2000CDs')
91 ->search({}, { bind => [2001] })
98 =item is_virtual set to false
100 $schema->resultset('Year2000CDs')->all();
102 SELECT cdid, artist, title FROM year2000cds me
104 =item is_virtual set to true
106 $schema->resultset('Year2000CDs')->all();
108 SELECT cdid, artist, title FROM
109 (SELECT cdid, artist, title FROM cd WHERE year ='2000') me
117 __PACKAGE__->result_source->is_virtual(1);
119 Set to true for a virtual view, false or unset for a real
122 =head2 view_definition
124 __PACKAGE__->result_source->view_definition(
125 "SELECT cdid, artist, title FROM cd WHERE year ='2000'"
128 An SQL query for your view. Will not be translated across database
131 =head2 deploy_depends_on
133 __PACKAGE__->result_source->deploy_depends_on(
134 ["MyApp::Schema::Result::Year","MyApp::Schema::Result::CD"]
137 Specify the views (and only the views) that this view depends on.
138 Pass this an array reference of fully qualified result classes.
140 =head1 OVERRIDDEN METHODS
144 Returns the FROM entry for the table (i.e. the view name)
145 or the SQL as a subselect if this is a virtual view.
150 $_[0]->throw_exception('from() is not a setter method') if @_ > 1;
152 ? \( '(' . $_[0]->view_definition .')' )
166 my ( $self, @args ) = @_;
167 my $new = $self->next::method(@args);
168 $new->{deploy_depends_on} =
170 @{ $new->{deploy_depends_on} || [] } }
171 unless ref $new->{deploy_depends_on} eq 'HASH';
175 =head1 FURTHER QUESTIONS?
177 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
179 =head1 COPYRIGHT AND LICENSE
181 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
182 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
183 redistribute it and/or modify it under the same terms as the
184 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.