Gtk2-Ex-DbLinker
view release on metacpan or search on metacpan
lib/Gtk2/Ex/DbLinker/Form.pm view on Meta::CPAN
=item *
pos 0 is the date format of the database.
=item *
pos 1 is the format to display the date in the form.
=back
C<time_zone> and C<locale> are needed by Date::Time::Strptime.
To display new rows on a bound subform, connect the on_change event to the field of the primary key in the main form.
In this sub, call a sub to synchonize the form:
In the main form:
sub on_nofm_changed {
my $widget = shift;
my $self = shift;
my $pk_value = $widget->get_text();
$self->{subform_a}->synchronize_with($pk_value);
...
}
In the subform_a module
sub synchronize_with {
my ($self,$value) = @_;
my $data = Rdb::Product::Manager->get_product(with_objects => ['seller_product'], query => ['seller_product.no_seller' => {eq => $value}]);
$self->{subform_a}->get_data_manager->query($data);
$self->{subform_a}->update;
}
=head2 Dealing with many to many relationship
It's the sellers and products situation where a seller sells many products and a product is selled by many sellers.
One way is to have a insert statement that insert a new row in the linking table (named transaction for example) each time a new row is added in the product table.
An other way is to create a data manager for the transaction table
With DBI
$dman = Gtk2::Ex::DbLinker::DbiDataManager->new( dbh => $self->{dbh}, sql =>{select =>"no_seller, no_product", from => "transaction", where => ""});
With Rose::DB::Object
$data = Rdb::Transaction::Manager->get_transaction(query=> [no_seller => {eq => $current_seller }]);
$dman = Gtk2::Ex::DbLinker::RdbDataManager->new(data => $data, meta=> Rdb::Transaction->meta);
And keep a reference of this for latter
$self->{linking_data} = $dman;
If you want to link a new row in the table product with the current seller, create a method that is passed and array of primary key values for the current seller and the new product.
sub update_linking_table {
my ( $self, $keysref) = @_;
my @keys = keys %{$keysref};
my $f = $self->{main_form};
my $dman = $self->{main_abo}->{linking_data};
$dman->new_row;
foreach my $k (@keys){
my $value = ${$keysref}{$k};
$dman->set_field($k, $value );
}
$dman->save;
}
This method is to be called when a new row has been added to the product table:
sub on_newproduct_applied_clicked {
my $button = shift;
my $self = shift;
my $main = $f->{main_form};
$self->{product}->apply;
my %h;
$h{no_seller}= $main->{no_seller};
$h{no_product}= $self->{abo}->get_widget_value("no_product");
$self->update_linking_table(\%h);
}
You may use the same method to delete a row from the linking table
my $data = Rdb::Transaction::Manager->get_transaction(query=> [no_seller => {eq => $seller }, no_product=>{eq => $product } ] );
$f->{linking_data}->query($data);
$f->{linking_data}->delete;
=head1 DESCRIPTION
This module automates the process of tying data from a database to widgets on a Glade-generated form.
All that is required is that you name your widgets the same as the fields in your data source.
Steps for use:
=over
=item *
Create a xxxDataManager object that contains the rows to display
=item *
Create a Gtk2::GladeXML object (the form widget)
=item *
Create a Gtk2::Ex::DbLinker::Form object that links the data and your form
=item *
You would then typically connect the buttons to the methods below to handle common actions
such as inserting, moving, deleting, etc.
=back
=head1 METHODS
( run in 3.048 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )