View on
Marcus Ramberg > DBIx-Class-0.05999_04 > 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)



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


Use this to set the name of the accessor 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 the data_type, DBIx::Class will attempt to retrieve it from the database for you, using DBIs 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(s) support.


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


If the column is allowed to contain NULL values, set a true value (typically 1), here. This is currently not used by DBIx::Class.


Set this to a true value if this is a column that is somehow automatically filled. This is currently not used by DBIx::Class.


Set this to a true value if this column represents a key from a foreign table. This is currently not used by DBIx::Class.


Set this to the default value which will be inserted into this column by the database. Can contain either values or functions. This is currently not used by DBIx::Class.


If your column is using a sequence to create it's values, set the name of the sequence here, to allow the values to be retrieved automatically by the DBIx::Class::PK::Auto module. PK::Auto will attempt to retrieve the sequence name from the database, if this value is left unset.


  $table->add_column('col' => \%info?);

Convenience alias to add_columns


  if ($obj->has_column($col)) { ... }

Returns 1 if the source has a column of this name, 0 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.


  my @column_names = $obj->columns;

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


Arguments: (@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.

The primary key columns are used by DBIx::Class::PK::Auto 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. Unique constraints are used when you call find on a DBIx::Class::ResultSet, only columns in the constraint are searched,

  # For e.g. UNIQUE (column1, column2)
  __PACKAGE__->add_unique_constraint(constraint_name => [ qw/column1 column2/ ]);


Read-only accessor which returns the list of unique constraints on this source.


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 storage handle for the current schema.

See also: DBIx::Class::Storage


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

  __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

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.


Returns all valid relationship names for this source


Arguments: ($relname)

Returns the relationship information for the specified relationship name


Arguments: ($rel)

Returns 1 if the source has a relationship of this name, 0 otherwise.


Arguments: ($relation)

Returns the join structure required for the related result source


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)

Returns the result source object for the given relationship


Returns a resultset for the given source, by calling:

  $self->resultset_class->new($self, $self->resultset_attributes)


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.


Specify here any attributes you wish to pass to your specialised resultset.


See throw_exception in DBIx::Class::Schema.


Matt S. Trout <>


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

syntax highlighting: