The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

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

$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. 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 ", ". 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 and view that does not have any default valu, so those two has to be defined within the table declaration.

$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->rel_post_view($p)

Will generate a view of the post $p, as described by the view parameter specified in the $data parameter to the constructor.

$fld->view_text =head2 $fld->view_html

Will view the related posts as defined by the view and join_str parameters specified in the $data parameter to the constructor

$fld->edit_html

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

$fld->del($id)

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

$fld->name_vals

Updates the link table and returns a empthy array as no fields in the from table should be modifed.

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