Creating objects with Gideon
You can easily persist new object in any gideon data store by using the save
method
save([ %opts ])
To store a new object you just simply need to call the save
method on any newly created instance. And returns the object if the saving was successful or undef
if the saving operation failed.
my $new_person = People->new( age => 34, name => 'Mike' );
say 'Success' if $new_person->save;
Gideon has a plugin that would rise an exception in case the operation fails rather than returning undef
. Please refer to Gideon::Plugin::StrictMode for more details.
my $new_person = People->new( age => 34, name => 'Mike' );
try {
$new_person->save( -strict => 1 );
say 'Success';
}
catch {
say 'Failed';
};
In the case of objects persisted into RDB's and auto increment values, Gideon would auto populate the resulting id back into the object
my $new_person = People->new( age => 34, name => 'Mike' );
say $new_person->id; # undef
$new_person->save;
say $new_person->id; # '1'