Table: preferences
+----+------------------+----------------------------+
| id | name | value |
+----+------------------+----------------------------+
| 0 | filename | /path/to/file.txt |
| 1 | item_enabled | 1 |
| 2 | task_name | something |
+----+------------------+----------------------------+
####
my $row=DB::Prefs->search({ name => "filename" })->first;
print $row->value,$/;
$row->value("/new/path/to/file");
##
##
print DB::Prefs->filename,$/;
DB::Prefs->filename("/new/path/to/file");
##
##
package App::Prefs;
use base 'App';
use strict;
__PACKAGE__->table("preferences");
__PACKAGE__->columns( ALL => qw/ id name value/ );
sub AUTOLOAD {
my $self = shift;
my $newval = shift;
my $attr = our $AUTOLOAD;
$attr =~ s/.*:://;
return if($attr eq "DESTROY");
my $row = $self->search({ name => "$attr" })->first();
if(defined($newval)) {
if(!$row) {
$row=$self->insert({ name => "$attr" });
}
$row->value($newval);
$row->update();
}
return defined($row) ? $row->value : undef;
}