Clinton Wolfe
and 1 contributors




  # Add relationships to a ReluctantORM Class

  # Now you have:
  $pirate = Pirate->fetch_with_ship($pirate_id);
  @bipeds = Pirate->fetch_by_leg_count_with_ship(2);

  # Get info about the relationship
  $rel = Pirate->relationships('ship');

  $str = $rel->type();                 # 'has_one';
  $str = $rel->linked_class();         # 'Ship';
  $str = $rel->linking_class();        # 'Pirate';
  @fields = $rel->local_key_fields();  # fields in Pirate that link to Ship
  @fields = $rel->remote_key_fields(); # array of fields in Ship that link to Pirate
  $int = $rel->join_depth();           # 1

  # Class::ReluctantORM::SQL integration
  @sql_cols = $rel->additional_output_sql_columns();
  @cols = $rel->local_key_sql_columns();
  @cols = $rel->remote_key_sql_columns();
  @empty = $rel->join_local_key_sql_columns();  # always empty for HasOne
  @empty = $rel->join_remote_key_sql_columns(); # always empty for HasOne



$class->has_one(class => 'OtherClass', local_key => [colname,...], remote_key => [colname, ...], => 'key_column', method_name => 'some_name', read_only => 1);

Describes a (possibly optional) relationship between two classes/tables.

The local table should have a column (or columns) that act as foreign keys into the remote table. An accessor/mutator wil be created that provides access to the related object.

Additionally, a new constructor is created, named $class->fetch_with_METHOD. This constructor has the special feature that it performs an outer join and pre-fetches the named object. Finally, additional constructors named $class->fetch_by_ATTRIBUTE_with_METHOD will also be available via AUTOLOAD.

In the first form, OtherClass is taken to be the 'class' argument, and all other arguments are determined from that.


class (string classname, required)

The name of the remote ReluctantORM class.

local_key (string or arrayref)

The name of the foreign key column (or columns) in the local table. Optional - default is OtherClass->primary_key_columns().

remote_key (string or arrayref)

The name of the foreign key column (or columns) in the remote table. Optional - default is OtherClass->primary_key_columns().

method_name (string)

The name of the accessor/mutator method to be created. Optional - default is the lowercased and underscore-spaced version of the class name of OtherClass.

foreign_key (string, deprecated)

Deprecated synonym for local_key.

The mutator will set the corresponding local key column.

The accessor will display some behavior intended to help with scalability. If the value has already been fetched, it will be returned normally. If a trip to the database would be required, the method dies with an Class::ReluctantORM::Exception::Data::FetchRequired. You can then actually fetch the value using $instance->fetch_METHOD .

$str = $rel->type();

Returns 'has_one'.

$bool = $rel->is_has_one();

Returns true.

$int = $rel->join_depth();

Returns 1.

$str = $rel->join_type();

Returns 'LEFT OUTER'

$int = $rel->lower_multiplicity()

Returns 0.

$int = $rel->upper_multiplicity()

Returns 1.