josh803316 has asked for the wisdom of the Perl Monks concerning the following question:
#********************************************************************* +******** # public create # ************* # Uses: Saves the given object into the database # Params: HASH with new object attributes # Return: The inserted object, or undef if the Object can't be inse +rted #********************************************************************* +******** sub create { my $class = shift; my ($param) = @_; my $dbh = Jnms::Database::Control::DatabaseManager->get_database-> +{dbh}; my $id; my $error; # check if the required fields are there and insert on the db #print STDERR Dumper($class,$param); $class->_check_required_create_fields($param); eval{ $id = $class->_insert_into_database($param); $dbh->commit; }; if ($@) { warn "Transaction aborted because $@"; $error = $@; # now rollback to undo the incomplete changes # but do it in an eval{} as it may also fail eval { $dbh->rollback }; die "Transaction aborted because of $error"; # add other application on-error-clean-up code here } #return the inserted object return $class->find($id); } #********************************************************************* +******** # public list_all # *************** # Uses: gets all Objects that matches the child class type from th +e db # Params: None. # Return: an ARRAY of all Objects of the requested type #********************************************************************* +******** sub list_all { my $class = shift; my ($param) = @_; my $dbh = Jnms::Database::Control::DatabaseManager->get_database-> +{dbh}; # get the constants specified on the child class my $columns = join(', ', $class->TABLE_COLUMNS); my $table = $class->TABLE_NAME; my $order = $class->LIST_ORDER; my @column_values = (); my $objects = $dbh->selectall_arrayref("SELECT $columns FROM $tabl +e ORDER BY $order", {Slice=>{}}, @column_values); bless($_, ref($class) || $class) foreach @$objects; my $depth = 0; if (ref $param eq 'HASH') { $depth = $param->{depth}; } elsif($param) { $depth = $param; } unless(defined $depth){ $depth = 0; } $objects = $class->_find_foreign_keys($objects, $depth); return $objects; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Mysql query cache and DBI
by graff (Chancellor) on Nov 07, 2009 at 07:17 UTC | |
by josh803316 (Beadle) on Nov 07, 2009 at 19:44 UTC | |
by josh803316 (Beadle) on Nov 07, 2009 at 21:00 UTC | |
by graff (Chancellor) on Nov 07, 2009 at 23:58 UTC | |
by reinaldo.gomes (Beadle) on Aug 14, 2018 at 06:28 UTC | |
|
Re: Mysql query cache and DBI
by bichonfrise74 (Vicar) on Nov 07, 2009 at 00:20 UTC | |
by josh803316 (Beadle) on Nov 07, 2009 at 00:42 UTC | |
by josh803316 (Beadle) on Nov 07, 2009 at 02:18 UTC |