DBIx::HTMLView::N2N - A many to many relation between two tabels


NAME

  DBIx::HTMLView::N2N - A many to many relation between two tabels


SYNOPSIS

  $fld=$post->fld('testf');
  print $fld->view_html;


DESCRIPTION

This is a subcalss of DBIx::HTMLView::Relation used to represent N2N relations in the databse as well as the data contained in them. Se the DBIx::HTMLView::Relation and DBIx::HTMLView:.Fld (the superclass of Relation) manpage for info on the methods of this class.

A N2N relation as where each post in one table can be related to any number of posts in an other table. As for example in the User/Group table pair example described in the tutorial where each user can be part of several groups.

A third table, called link table, will be used to represent the relations. It should contain three fields. One id field (as all tabels), one from id (eg user id) and one to id (eg group id). Now one relation consists of a set of posts in this table each linking one from post (eg user) to one to post (eg group).

As for the overall operation this kind of Flds should wokr like any other Fld, but you can also do a few extra things, as described below.


METHODS =cut

package DBIx::HTMLView::N2N; use strict; use Carp;

use vars qw(@ISA); require DBIx::HTMLView::Relation; @ISA = qw(DBIx::HTMLView::Relation);

require DBIx::HTMLView::Fmt;

$fld=DBIx::HTMLView::N2N->($name, $data) =head2 $fld=DBIx::HTMLView::N2N->new($name, $val, $tab)

The constructor works in the exakt same way as for DBIx::HTMLView::Fld, se it's man page for details.

The following parameters passed within the $data has is recognised:

tab - The table this table is related to (to table) from_field - The field name of the link table where the from table post id is stored. Default is ``<from table>_id''. to_field - The field name of the link table where the to table post id is stored. Default is ``<to table>_id''. lnk_tab - The name of the link table. Default is ``<from table>_to_<to table>''. id_name - The name of the link post id field in the link table. Default is ``id''. view - String used when viewing a related post withing the post being viewed (eg the groups list of a user post). All $<fld name> constructs will be replaced with the data of the post beeing viewed. Obsolete, use the fmt param instead. join_str - As a post can be related to several other and each will be viewed using the view string above and then joined together using this string as glue. Default is ``, ''. Obsolete, use the fmt param instead. fmt - Specifies the fmt string to be passed to view_fmt of the PostSet object representing the posts we are related to. For backwards compatibility it defaults to ``<node join=''$join_str``>$view</node>'', if $view is defined. $view and $join_str are the var defined above. extra_sql - Extra sql code passe to the list method when listing related posts. This can for example be used to specify in which order related posts should be viewed. Default is ``ORDER BY <to table id name>''.

As you se, it is only tab that does not have any default value, so it has to be defined within the table declaration, it's usually also a good idea to specify fmt to something decent as well.


=cut

sub new { my $this = shift; my $class = ref($this) || $this; my $self= bless {}, $class;

  my ($name, $data, $tab)=@_;
  $self->{'name'}=$name;
  $self->{'tab'}=$tab;

  if (ref $data eq "HASH") {
    $self->{'data'}=$data;
  } elsif (ref $data eq "ARRAY") {
    if (ref $this) {$self->{'data'}=$this->{'data'};}
    $self->{'posts'}=DBIx::HTMLView::PostSet->new($self->to_tab);
    foreach (@$data) {
      if ($_ ne "do_edit") {
        $self->{'posts'}->add($self->to_tab->new_post({$self->tab->id->name=>$_}));
      }
    }
  } else {
    $self->{'id'}=$data;
    if (ref $this) {$self->{'data'}=$this->{'data'};}
  }

  $self;
}

$fld->db

Returns the database handle of the tabels.

$fld->id

Returns the id of the post this relation belongs to.

$fld->to_tab_name

Returns the name of the to table.

$fld->to_tab

Returns the DBIx::HTMLView::Table object representing the to table.

$fld->from_field_name

Returns the name of the from field in the link table as specified in the $data param to the constructor.

$fld->to_field_name

Returns the name of the to field in the link table as specified in the $data param to the constructor.

$fld->lnk_tab_name

Returns the name of the link table as specified in the $data param to the constructor.

$fld->id_name

Returns the name of the link post id field in the link table as specified in the $data param to the constructor.

$fld->lnk_tab

Creates and returns a DBIx::HTMLView::Table object representing the link table.

$fld->join_str

Returns the join_str parameter as specified in the $data param to the constructor.

$fld->extra_sql

Returns the extra_sql parameter as specified in the $data param to the constructor.

$fld->got_post_set

Returns true if we have a post set. Se the post_set method.

$fld->post_set

When this object is used to represent the data of a relation that can be done in two ways. Either we just know the id of the post we belong to and can look up the related posts from the db whenever needed. When such a post lookup is done the (parts of the) posts returned are stored in a DBIx::HTMLView::PostSet object.

This method will return such an object after selecting it from the server if nesessery. You can use the got_post_set method to check if it was already donwloaded. If this Fld did not belong to a specifik post, eg no id was defiedn it will die with ``Post not defined!''.

$fld->posts

Will return an array of the posts after calling the post_set method. If there are no related posts it will not die, but return an empthy array.

$fld->view_fmt_edit_html($postfmt_name, $postfmt)

Used by the default edit_html fmt. It will return a string containing ``<input type=checkbox ...>'' constructs to allow the user to specify which posts we should be related to. All posts in the to table will be listed here and viewed with view_fmt($postfmt_name,$postfmt).

$postfmt_name will default to 'view_html'. If $postfmt isn't defined some decent default is tried to be derived from the default fmt for $postfmt_name.

The $postfmt should contain a <Var Edit> tag that will be raplaced by the checkbox button.

$fld->del($id)

Will remove the relation from post $id. Eg it will no longer be related to any posts.

$fld->name_vals

Returns an empthy array as no fields in the from table should be modifed.

$fld->post_updated

Updates the relation data in the db.

$fld->sql_data($sel, $sub)

Used by the DBIx::HTMLView::Selection object $sel when it finds a relation->field construct in a search string that should be evaled into an sql select expretion. $sub will be a refference to an array of all names after the -> signs, eg for rel1->rel2->rel3->field $sub would contain (``rel2'', ``rel3'', ``field'') and this would be the rel1 relation.

$fld->field_name

Returns undef as we've not got any field in the main table. Se DBIx::HTMLView::Fld.

$fld->view_fmt($fmt_name, $fmt)

Will call view_fmt($fmt_name, $fmt) on the postset containing all the posts this relation is pointing to and return the result, see DBIx::HTMLView::PostSet for info on the $fmt format.

If the fmt string starts with ``<InRel>'', the rest of the fmt will be handled by this method instead of calling the PostSet version. Current the only supported construct here is <perl>...</perl> which will be replaced by the returnvalue of eval(...).

 DBIx::HTMLView::N2N - A many to many relation between two tabels