NAME
DBIx::Class::ResultSource - Result source object
SYNOPSIS
DESCRIPTION
A ResultSource is a component of a schema from which results can be directly retrieved, most usually a table (see DBIx::Class::ResultSource::Table)
METHODS
add_columns
$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.
add_column
$table->add_column('col' => \%info?);
Convenience alias to add_columns
has_column
if ($obj->has_column($col)) { ... }
Returns 1 if the source has a column of this name, 0 otherwise.
column_info
my $info = $obj->column_info($col);
Returns the column metadata hashref for a column.
columns
my @column_names = $obj->columns;
Returns all column names in the order they were declared to add_columns
set_primary_key(@cols)
Defines one or more columns as primary key for this source. Should be called after add_columns
.
Additionally, defines a unique constraint named primary
.
primary_columns
Read-only accessor which returns the list of primary keys.
add_unique_constraint
Declare a unique constraint on this source. Call once for each unique constraint.
# For e.g. UNIQUE (column1, column2)
__PACKAGE__->add_unique_constraint(constraint_name => [ qw/column1 column2/ ]);
unique_constraints
Read-only accessor which returns the list of unique constraints on this source.
from
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.
storage
Returns the storage handle for the current schema
add_relationship
$source->add_relationship('relname', 'related_source', $cond, $attrs);
The relation 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 simple schemas this is usally either Some::Namespace::Foo or just Foo)
The condition needs to be an SQL::Abstract-style representation of the join between the tables. For example, if you're creating a rel from Foo to Bar,
{ 'foreign.foo_id' => 'self.id' }
will result in the JOIN clause
foo me JOIN bar bar ON bar.foo_id = me.id
You can specify as many foreign => self mappings as necessary.
Valid attributes are as follows:
- join_type
-
Explicitly specifies the type of join to use in the relationship. Any SQL join type is valid, e.g.
LEFT
orRIGHT
. It will be placed in the SQL command immediately beforeJOIN
. - proxy
-
An arrayref containing a list of accessors in the foreign class to proxy in the main class. If, for example, you do the following:
__PACKAGE__->might_have(bar => 'Bar', undef, { proxy => [ qw/margle/ ] });
Then, assuming Bar has an accessor named margle, you can do:
my $obj = Foo->find(1); $obj->margle(10); # set margle; Bar object is created if it doesn't exist
- accessor
-
Specifies the type of accessor that should be created for the relationship. Valid values are
single
(for when there is only a single related object),multi
(when there can be many), andfilter
(for when there is a single related object, but you also want the relationship accessor to double as a column accessor). Formulti
accessors, an add_to_* method is also created, which callscreate_related
for the relationship.
relationships()
Returns all valid relationship names for this source
relationship_info($relname)
Returns the relationship information for the specified relationship name
has_relationship($rel)
Returns 1 if the source has a relationship of this name, 0 otherwise.
resolve_join($relation)
Returns the join structure required for the related result source
resolve_condition($cond, $as, $alias|$object)
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.
resolve_prefetch (hashref/arrayref/scalar)
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'
#)
related_source($relname)
Returns the result source for the given relationship
resultset
Returns a resultset for the given source created by calling
$self->resultset_class->new($self, $self->resultset_attributes)
resultset_class
Simple accessor.
resultset_attributes
Simple accessor.
throw_exception
See schema's throw_exception
AUTHORS
Matt S. Trout <mst@shadowcatsystems.co.uk>
LICENSE
You may distribute this code under the same terms as Perl itself.