DBIx-DBO2

 view release on metacpan or  search on metacpan

DBO2/Fields.pm  view on Meta::CPAN

########################################################################

=head2 Field Type line_items

Generates methods to retrieve records from another table which have a foreign_key relationship to the current record. Depends on there being a primary key column, but does not require a separate database column of its own.

=head3 Default Interface

The general usage for a line_items field is:

  package Y;
  use DBIx::DBO2::Fields (
    line_items => { name=>'x', 'related_field'=>'y_id', related_class=>'X' },
  );

This declaration will generate methods to support the following interface:

=over 4

=item *

I<$record>-E<gt>x() : I<related_objects>

Fetches the related records. Returns a RecordSet.

=item *

I<$record>-E<gt>x( I<rel_col> => I<rel_value>, ...) : I<related_objects>

Fetches a subset of the related records which also meet the indicated criteria. Returns a RecordSet.

=item *

I<$record>-E<gt>count_x() 

Returns the number or related records.

=item *

I<$record>-E<gt>new_x() : I<related_object>

Creates and returns a new related record, setting its foreign key field to refer to our record's ID. 

(Note that the record is created but not inserted; you need to call -E<gt>save() yourself.)

=item *

I<$record>-E<gt>delete_x()

Deletes B<all> of the related records. 

=back

You can also specify an array-ref value for the default_criteria attribute; if present, it is treated as a list of fieldname/value pairs to be passed to the fetch and new methods of the related class.


=head3 restrict_delete Interface

Identical to the default interface except as follows: an ok_delete hook is installed to check for the existance of any related records, in which case the deletion is cancelled. This prevents you from deleting the "parent" record for a number of relat...

=head3 cascade_delete Interface

Identical to the default interface except as follows: a post_delete hook is installed to delete all of the related records after the parent record is deleted.

=head3 nullify_delete Interface

Identical to the default interface except as follows: a post_delete hook is installed to change all of the related records to have a default value.

=cut

sub line_items {
  {
    '-import' => { '::DBIx::DBO2::Fields:generic' => '*' },
    'params' => {
      'id_method' => 'id',
      'related_class' => undef,
      'related_field' => undef,
      'default_criteria' => undef,
      'default_order' => 'id',
    },
    'interface' => {
      default	    => { 
	'*'=>'fetch', 
	'count_*'=>'count', 
	'new_*'=>'new', 
	'delete_*'=>'delete',
      },
      restrict_delete => { 
	-base => 'default', 
	'check_for_*'=>'check_count', 
	-params => {
	  hook => { ok_delete => 'check_for_*' }
	},
      },
      nullify_on_delete  => { 
	-base => 'default', 
	'nullify_*'=>'nullify', 
	-params => {
	  delete_default => undef,
	  hook => { post_delete => 'nullify_*' }
	},
      },
      cascade_delete  => { 
	-base => 'default', 
	-params => {
	  hook => { post_delete => 'delete_*' }
	},
      },
    },
    'behavior' => {
      'fetch' => q{
	  my $related = _ATTR_REQUIRED_{related_class};

	  my %params = @_;
	  
	  my $r_field = _ATTR_REQUIRED_{related_field};
	  my $id_method = _ATTR_REQUIRED_{id_method};
	  my $d_crit = _ATTR_{default_criteria};
	  $d_crit = { @$d_crit } if ( ref($d_crit) eq 'ARRAY' );
	  my $id = $self->$id_method()
		or return DBIx::DBO2::RecordSet->new();
	  my $criteria = DBIx::SQLEngine::Criteria->auto_and(
	      DBIx::SQLEngine::Criteria::Equality->new($r_field=>$id),
	      ( $d_crit || () ), 
	      ( $params{criteria} ? $params{criteria} : () ),
	  );
	  $params{criteria} = $criteria;
	  if ( my $default_order = _ATTR_{default_order} ) {
	    $params{order} ||= $default_order
	  }
	  $related->fetch_records(%params);
	},
      'count' => q{
	  my $related = _ATTR_REQUIRED_{related_class};

	  my %params = @_;
	  
	  my $r_field = _ATTR_REQUIRED_{related_field};
	  my $id_method = _ATTR_REQUIRED_{id_method};
	  my $d_crit = _ATTR_{default_criteria};
	  $d_crit = { @$d_crit } if ( ref($d_crit) eq 'ARRAY' );
	  my $id = $self->$id_method()
		or return 0;
	  # warn "Counting from " . $related->table->name;
	  my $criteria = DBIx::SQLEngine::Criteria->auto_and(
	      DBIx::SQLEngine::Criteria::Equality ->new($r_field=>$id),
	      ( $d_crit || () ), 
	      ( $params{criteria} ? $params{criteria} : () ),
	  );
	  delete $params{criteria};
	  $related->table->count_rows($criteria);
 	},
      'delete' => q{
	  my $fetch_method = _STATIC_ATTR_{name};
	  foreach my $item ( $self->$fetch_method(@_)->records ) {
	    $item->delete_record();
	  }
 	},
      'nullify' => q{
	  my $fetch_method = _STATIC_ATTR_{name};
	  my $r_field = _ATTR_REQUIRED_{related_field};
	  my $default = _ATTR_{delete_default};
	  foreach my $item ( $self->$fetch_method(@_)->records ) {



( run in 1.090 second using v1.01-cache-2.11-cpan-bbc515a03b3 )