DBIx::Class::ResultSource - Result source object |
DBIx::Class::ResultSource - Result source object
A ResultSource is a component of a schema from which results can be directly retrieved, most usually a table (see the DBIx::Class::ResultSource::Table manpage)
$class->new();
$class->new({attribute_name => value});
Creates a new ResultSource object. Not normally called directly by end users.
Stores a hashref of per-source metadata. No specific key names have yet been standardized, the examples below are purely hypothetical and don't actually accomplish anything on their own:
__PACKAGE__->source_info({ "_tablespace" => 'fast_disk_array_3', "_engine" => 'InnoDB', });
$table->add_columns(qw/col1 col2 col3/);
$table->add_columns('col1' => \%col1_info, 'col2' => \%col2_info, ...);
Adds columns to the result source. If supplied key => hashref pairs, uses the hashref as the column_info for that column. Repeated calls of this method will add more columns, not replace them.
The contents of the column_info are not set in stone. The following keys are currently recognised/used by DBIx::Class:
Currently there is no standard set of values for the data_type. Use whatever your database supports.
copy
. It is also used by
deploy in the DBIx::Class::Schema manpage.
extra
=> { unsigned => 1}
is used by the MySQL producer to set an integer
column to unsigned. For more details, see
the SQL::Translator::Producer::MySQL manpage.
$table->add_column('col' => \%info?);
Convenience alias to add_columns.
if ($obj->has_column($col)) { ... }
Returns true if the source has a column of this name, false otherwise.
my $info = $obj->column_info($col);
Returns the column metadata hashref for a column. See the description of add_column for information on the contents of the hashref.
Enables the on-demand automatic loading of the above column metadata from storage as neccesary. This is *deprecated*, and should not be used. It will be removed before 1.0.
__PACKAGE__->column_info_from_storage(1);
my @column_names = $obj->columns;
Returns all column names in the order they were declared to add_columns.
$table->remove_columns(qw/col1 col2 col3/);
Removes columns from the result source.
$table->remove_column('col');
Convenience alias to remove_columns.
Defines one or more columns as primary key for this source. Should be
called after add_columns
.
Additionally, defines a unique constraint named primary
.
The primary key columns are used by the DBIx::Class::PK::Auto manpage to retrieve automatically created values from the database.
Read-only accessor which returns the list of primary keys.
Declare a unique constraint on this source. Call once for each unique constraint.
# For UNIQUE (column1, column2) __PACKAGE__->add_unique_constraint( constraint_name => [ qw/column1 column2/ ], );
Alternatively, you can specify only the columns:
__PACKAGE__->add_unique_constraint([ qw/column1 column2/ ]);
This will result in a unique constraint named table_column1_column2
, where
table
is replaced with the table name.
Unique constraints are used, for example, when you call find in the DBIx::Class::ResultSet manpage. Only columns in the constraint are searched.
Return a name for a unique constraint containing the specified columns. These names consist of the table name and each column name, separated by underscores.
For example, a constraint on a table named cd
containing the columns
artist
and title
would result in a constraint name of cd_artist_title
.
Read-only accessor which returns the list of unique constraints on this source.
Returns the list of unique constraint names defined on this source.
Returns the list of columns that make up the specified unique constraint.
Returns an expression of the source to be supplied to storage to specify retrieval from this source. In the case of a database, the required FROM clause contents.
Returns the the DBIx::Class::Schema manpage object that this result source belongs too.
Returns the storage handle for the current schema.
See also: the DBIx::Class::Storage manpage
$source->add_relationship('relname', 'related_source', $cond, $attrs);
The relationship name can be arbitrary, but must be unique for each relationship attached to this result source. 'related_source' should be the name with which the related result source was registered with the current schema. For example:
$schema->source('Book')->add_relationship('reviews', 'Review', { 'foreign.book_id' => 'self.id', });
The condition $cond
needs to be an the SQL::Abstract manpage-style
representation of the join between the tables. For example, if you're
creating a rel from Author to Book,
{ 'foreign.author_id' => 'self.id' }
will result in the JOIN clause
author me JOIN book foreign ON foreign.author_id = me.id
You can specify as many foreign => self mappings as necessary.
Valid attributes are as follows:
LEFT
or RIGHT
. It will be placed in
the SQL command immediately before JOIN
.
CD->might_have(liner_notes => 'LinerNotes', undef, { proxy => [ qw/notes/ ], });
Then, assuming LinerNotes has an accessor named notes, you can do:
my $cd = CD->find(1); # set notes -- LinerNotes object is created if it doesn't exist $cd->notes('Notes go here');
single
(for when there is only a single
related object), multi
(when there can be many), and filter
(for
when there is a single related object, but you also want the relationship
accessor to double as a column accessor). For multi
accessors, an
add_to_* method is also created, which calls create_related
for the
relationship.
Returns all relationship names for this source.
Returns a hash of relationship information for the specified relationship name.
Returns true if the source has a relationship of this name, false otherwise.
Returns an array of hash references of relationship information for the other side of the specified relationship name.
Returns true if both sets of keynames are the same, false otherwise.
Returns the join structure required for the related result source.
Resolves the passed condition to a concrete query fragment. If given an alias, returns a join condition; if given an object, inverts that object to produce a related conditional from that object.
Accepts one or more relationships for the current source and returns an array of column names for each of those relationships. Column names are prefixed relative to the current source, in accordance with where they appear in the supplied relationships. Examples:
my $source = $schema->resultset('Tag')->source; @columns = $source->resolve_prefetch( { cd => 'artist' } );
# @columns = #( # 'cd.cdid', # 'cd.artist', # 'cd.title', # 'cd.year', # 'cd.artist.artistid', # 'cd.artist.name' #)
@columns = $source->resolve_prefetch( qw[/ cd /] );
# @columns = #( # 'cd.cdid', # 'cd.artist', # 'cd.title', # 'cd.year' #)
$source = $schema->resultset('CD')->source; @columns = $source->resolve_prefetch( qw[/ artist producer /] );
# @columns = #( # 'artist.artistid', # 'artist.name', # 'producer.producerid', # 'producer.name' #)
Returns the result source object for the given relationship.
Returns the class name for objects in the given relationship.
Returns a resultset for the given source. This will initially be created on demand by calling
$self->resultset_class->new($self, $self->resultset_attributes)
but is cached from then on unless resultset_class changes.
` package My::ResultSetClass; use base 'DBIx::Class::ResultSet'; ...
$source->resultset_class('My::ResultSet::Class');
Set the class of the resultset, this is useful if you want to create your own resultset methods. Create your own class derived from the DBIx::Class::ResultSet manpage, and set it here.
$source->resultset_attributes({ order_by => [ 'id' ] });
Specify here any attributes you wish to pass to your specialised resultset. For a full list of these, please see ATTRIBUTES in the DBIx::Class::ResultSet manpage.
Set the name of the result source when it is loaded into a schema. This is usefull if you want to refer to a result source by a name other than its class name.
package ArchivedBooks; use base qw/DBIx::Class/; __PACKAGE__->table('books_archive'); __PACKAGE__->source_name('Books');
# from your schema... $schema->resultset('Books')->find(1);
Obtain a new handle to this source. Returns an instance of a the DBIx::Class::ResultSourceHandle manpage.
See throw_exception in the DBIx::Class::Schema manpage.
Matt S. Trout <mst@shadowcatsystems.co.uk>
You may distribute this code under the same terms as Perl itself.
DBIx::Class::ResultSource - Result source object |