View on
Matt S Trout > DBIx-Class-0.08100 > DBIx::Class::ResultSource



Annotate this POD (1)


New  24
Open  40
Stalled  13
View/Report Bugs
Source   Latest Release: DBIx-Class-0.082841


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 DBIx::Class::ResultSource::Table)

Basic view support also exists, see <DBIx::Class::ResultSource::View.



Arguments: @columns
Return value: The ResultSource object
  $source->add_columns(qw/col1 col2 col3/);

  $source->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 column names given will be created as accessor methods on your DBIx::Class::Row objects. You can change the name of the accessor by supplying an "accessor" in the column_info hash.

The contents of the column_info are not set in stone. The following keys are currently recognised/used by DBIx::Class:


Use this to set the name of the accessor method for this column. If unset, the name of the column will be used.


This contains the column type. It is automatically filled by the SQL::Translator::Producer::DBIx::Class::File producer, and the DBIx::Class::Schema::Loader module. If you do not enter a data_type, DBIx::Class will attempt to retrieve it from the database for you, using DBI's column_info method. The values of this key are typically upper-cased.

Currently there is no standard set of values for the data_type. Use whatever your database supports.


The length of your column, if it is a column type that can have a size restriction. This is currently only used by "deploy" in DBIx::Class::Schema.


Set this to a true value for a columns that is allowed to contain NULL values. This is currently only used by "deploy" in DBIx::Class::Schema.


Set this to a true value for a column whose value is somehow automatically set. This is used to determine which columns to empty when cloning objects using copy. It is also used by "deploy" in DBIx::Class::Schema.


Set this to a true value for a column that contains a key from a foreign table. This is currently only used by "deploy" in DBIx::Class::Schema.


Set this to the default value which will be inserted into a column by the database. Can contain either a value or a function (use a reference to a scalar e.g. \'now()' if you want a function). This is currently only used by "deploy" in DBIx::Class::Schema.

See the note on "new" in DBIx::Class::Row for more information about possible issues related to db-side default values.


Set this on a primary key column to the name of the sequence used to generate a new key value. If not specified, DBIx::Class::PK::Auto will attempt to retrieve the name of the sequence from the database automatically.


Set this to a true value for a column whose value is retrieved automatically from an oracle sequence. If you do not use an Oracle trigger to get the nextval, you have to set sequence as well.


This is used by "deploy" in DBIx::Class::Schema and SQL::Translator to add extra non-generic data to the column. For example: extra => { unsigned => 1} is used by the MySQL producer to set an integer column to unsigned. For more details, see SQL::Translator::Producer::MySQL.


Arguments: $colname, [ \%columninfo ]
Return value: 1/0 (true/false)
  $source->add_column('col' => \%info?);

Add a single column and optional column info. Uses the same column info keys as "add_columns".


Arguments: $colname
Return value: 1/0 (true/false)
  if ($source->has_column($colname)) { ... }

Returns true if the source has a column of this name, false otherwise.


Arguments: $colname
Return value: Hashref of info
  my $info = $source->column_info($col);

Returns the column metadata hashref for a column, as originally passed to "add_columns". See the description of "add_columns" for information on the contents of the hashref.


Arguments: None
Return value: Ordered list of column names
  my @column_names = $source->columns;

Returns all column names in the order they were declared to "add_columns".


Arguments: @colnames
Return value: undefined
  $source->remove_columns(qw/col1 col2 col3/);

Removes the given list of columns by name, from the result source.

Warning: Removing a column that is also used in the sources primary key, or in one of the sources unique constraints, will result in a broken result source.


Arguments: $colname
Return value: undefined

Remove a single column by name from the result source, similar to "remove_columns".

Warning: Removing a column that is also used in the sources primary key, or in one of the sources unique constraints, will result in a broken result source.


Arguments: @cols
Return value: undefined

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 DBIx::Class::PK::Auto to retrieve automatically created values from the database.


Arguments: None
Return value: Ordered list of primary column names

Read-only accessor which returns the list of primary keys, supplied by "set_primary_key".


Arguments: [ $name ], \@colnames
Return value: undefined

Declare a unique constraint on this source. Call once for each unique constraint.

  # For UNIQUE (column1, column2)
    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 DBIx::Class::ResultSet. Only columns in the constraint are searched.

Throws an error if any of the given column names do not yet exist on the result source.


Arguments: @colnames
Return value: Constraint name
  $source->name_unique_constraint('col1', 'col2');
  # returns

Return a name for a unique constraint containing the specified columns. The name is created by joining the table name and each column name, using an underscore character.

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.

This is used by "add_unique_constraint" if you do not specify the optional constraint name.


Arguments: None
Return value: Hash of unique constraint data

Read-only accessor which returns a hash of unique constraints on this source.

The hash is keyed by constraint name, and contains an arrayref of column names as values.


Arguments: None
Return value: Unique constraint names

Returns the list of unique constraint names defined on this source.


Arguments: $constraintname
Return value: List of constraint columns

Returns the list of columns that make up the specified unique constraint.


Arguments: $callback

An accessor to set a callback to be called during deployment of the schema via "create_ddl_dir" in DBIx::Class::Schema or "deploy" in DBIx::Class::Schema.

The callback can be set as either a code reference or the name of a method in the current result class.

If not set, the "default_sqlt_deploy_hook" is called.

Your callback will be passed the $source object representing the ResultSource instance being deployed, and the SQL::Translator::Schema::Table object being created from it. The callback can be used to manipulate the table object or add your own customised indexes. If you need to manipulate a non-table object, use the "sqlt_deploy_hook" in DBIx::Class::Schema.

See "Adding Indexes And Functions To Your SQL" in DBIx::Class::Manual::Cookbook for examples.

This sqlt deployment callback can only be used to manipulate SQL::Translator objects as they get turned into SQL. To execute post-deploy statements which SQL::Translator does not currently handle, override "deploy" in DBIx::Class::Schema in your Schema class and call dbh_do.


Arguments: $source, $sqlt_table
Return value: undefined

This is the sensible default for "sqlt_deploy_callback".

If a method named sqlt_deploy_hook exists in your Result class, it will be called and passed the current $source and the $sqlt_table being deployed.


Arguments: None
Return value: $resultset

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.


Arguments: $classname
Return value: $classname
  package My::ResultSetClass;
  use base 'DBIx::Class::ResultSet';


Set the class of the resultset. This is useful if you want to create your own resultset methods. Create your own class derived from DBIx::Class::ResultSet, and set it here. If called with no arguments, this method returns the name of the existing resultset class, if one exists.


Arguments: \%attrs
Return value: \%attrs
  $source->resultset_attributes({ order_by => [ 'id' ] });

Store a collection of resultset attributes, that will be set on every DBIx::Class::ResultSet produced from this result source. For a full list see "ATTRIBUTES" in DBIx::Class::ResultSet.


Arguments: $source_name
Result value: $source_name

Set an alternate name for the result source when it is loaded into a schema. This is useful if you want to refer to a result source by a name other than its class name.

  package ArchivedBooks;
  use base qw/DBIx::Class/;

  # from your schema...


Arguments: None
Return value: FROM clause
  my $from_clause = $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.


Arguments: None
Return value: A schema object
  my $schema = $source->schema();

Returns the DBIx::Class::Schema object that this result source belongs to.


Arguments: None
Return value: A Storage object

Returns the storage handle for the current schema.

See also: DBIx::Class::Storage


Arguments: $relname, $related_source_name, \%cond, [ \%attrs ]
Return value: 1/true if it succeeded
  $source->add_relationship('relname', 'related_source', $cond, $attrs);

DBIx::Class::Relationship describes a series of methods which create pre-defined useful types of relationships. Look there first before using this method directly.

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' => '',

The condition $cond needs to be an SQL::Abstract-style representation of the join between the tables. For example, if you're creating a relation from Author to Book,

  { 'foreign.author_id' => '' }

will result in the JOIN clause

  author me JOIN book foreign ON foreign.author_id =

You can specify as many foreign => self mappings as necessary.

Valid attributes are as follows:


Explicitly specifies the type of join to use in the relationship. Any SQL join type is valid, e.g. LEFT or RIGHT. It will be placed in the SQL command immediately before JOIN.


An arrayref containing a list of accessors in the foreign class to proxy in the main class. If, for example, you do the following:

  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');

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), 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.

Throws an exception if the condition is improperly supplied, or cannot be resolved using "resolve_join".


Arguments: None
Return value: List of relationship names
  my @relnames = $source->relationships();

Returns all relationship names for this source.


Arguments: $relname
Return value: Hashref of relation data,

Returns a hash of relationship information for the specified relationship name. The keys/values are as specified for "add_relationship".


Arguments: $rel
Return value: 1/0 (true/false)

Returns true if the source has a relationship of this name, false otherwise.


Arguments: $relname
Return value: Hashref of relationship data

Looks through all the relationships on the source this relationship points to, looking for one whose condition is the reverse of the condition on this relationship.

A common use of this is to find the name of the belongs_to relation opposing a has_many relation. For definition of these look in DBIx::Class::Relationship.

The returned hashref is keyed by the name of the opposing relationship, and contains it's data in the same manner as "relationship_info".


Arguments: \@keys1, \@keys2
Return value: 1/0 (true/false)

Returns true if both sets of keynames are the same, false otherwise.


Arguments: $relation
Return value: Join condition arrayref

Returns the join structure required for the related result source.


Arguments: $relname, $rel_data
Return value: 1/0 (true/false)

Determines whether a relation is dependent on an object from this source having already been inserted. Takes the name of the relationship and a hashref of columns of the related object.


Arguments: $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.


Arguments: 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',
  #  ''

  @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',
  #  '',
  #  'producer.producerid',
  #  ''


Arguments: $relname
Return value: $source

Returns the result source object for the given relationship.


Arguments: $relname
Return value: $classname

Returns the class name for objects in the given relationship.


Obtain a new handle to this source. Returns an instance of a DBIx::Class::ResultSourceHandle.


See "throw_exception" in DBIx::Class::Schema.


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:

    "_tablespace" => 'fast_disk_array_3',
    "_engine" => 'InnoDB',



  $class->new({attribute_name => value});

Creates a new ResultSource object. Not normally called directly by end users.


Arguments: 1/0 (default: 0)
Return value: 1/0

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.


Matt S. Trout <>


You may distribute this code under the same terms as Perl itself.

syntax highlighting: