EnsEMBL::Web::User::Record
module.
EnsEMBL::Web::User::Record
class is responsible for saving, loading and modifying data saved by the user. The module can handle multiple types of data, and can store all Perl data types (strings, arrays and hashes) along with blessed objects.
Record
class is based on the Active Record pattern. Each Record
object wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data 1.
Record
object. For example, to add a new bookmark:
sub add_bookmark {
my ($self, %params) = @_;
my $user_id = $ENV{ENSEMBL_USER_ID};
my $record = EnsEMBL::Web::User::Record->new(( adaptor => $params{adaptor} ));
$record->user($params{user_id});
$record->type('bookmarks');
$record->name($params{name});
$record->url($params{url});
$record->save;
}
Upon the save
method, the record is committed to the database.
save
method call will update the existing record. Renaming our bookmark, for example, is straightforward:
sub update_bookmark {
my ($self, $record, $new_name) = @_;
$record->name($new_name);
$record->save;
}
my $records = EnsEMBL::Web::User::Record->find_bookmark_by_user_id('42');
You could also search for records of the 'ticket' type with:
my $records = EnsEMBL::Web::User::Record->find_ticket_by_user_id('42');
User::Record
only has three predetermined parameters, all of which should be set before save is called:
User::Record
can be used to access any other parameter automatically just by calling a method of the same name on the object. All parameters will be repopulated when the user data is retrieved from the database (typically with a find_by class method call), and can be accessed again, with a method call.
$record->name
and $record->url
methods. No additional accessor glue code is required.
find_by
method automatically retrieves the record objects based upon the available saved parameters and supplied predicate. Whilst in the example above we return bookmarks by user id, we could also access a specific bookmark record via its own unique identifier:
sub get_bookmark {
my ($self, $id) = @_;
return EnsEMBL::Web::User::Record->find_bookmark_by_id($id);
}
© 2024 Inserm. Hosted by genouest.org. This product includes software developed by Ensembl.