DBIx::HTMLView::N2N - A many to many relation between two tabels |
del($id)
DBIx::HTMLView::N2N - A many to many relation between two tabels
$fld=$post->fld('testf'); print $fld->view_html;
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.
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;
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; }
Returns the database handle of the tabels.
Returns the id of the post this relation belongs to.
Returns the name of the to table.
Returns the DBIx::HTMLView::Table object representing the to table.
Returns the name of the from field in the link table as specified in the $data param to the constructor.
Returns the name of the to field in the link table as specified in the $data param to the constructor.
Returns the name of the link table as specified in the $data param to the constructor.
Returns the name of the link post id field in the link table as specified in the $data param to the constructor.
Creates and returns a DBIx::HTMLView::Table object representing the link table.
Returns the join_str parameter as specified in the $data param to the constructor.
Returns the extra_sql parameter as specified in the $data param to the constructor.
Returns true if we have a post set. Se the post_set method.
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!''.
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.
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.
del($id)
Will remove the relation from post $id. Eg it will no longer be related to any posts.
Returns an empthy array as no fields in the from table should be modifed.
Updates the relation data in the db.
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.
Returns undef as we've not got any field in the main table. Se DBIx::HTMLView::Fld.
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 |