/
Model.pm6
56 lines (39 loc) · 1.29 KB
/
Model.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use v6;
unit package ModelDB;
use ModelDB::Column;
=begin pod
=head1 NAME
ModelDB::Model - This is the base class for all models
=head1 DESCRIPTION
This class is inherited by every object declared with the C<model> keyword. It provides some common features to all model classes.
=head1 METHODS
=head2 method save-id
method save-id($id)
Sets the primary key field. This method should not be called except by ModelDB components. It is only called when a row is created.
=end pod
class Model {
method new(|c) {
my $c = c;
if c.hash<sql-load> {
my %hash;
for c.hash.kv -> $c, $v {
if (my $attr = self.^attributes.first({ .name eq '$!' ~ $c })) ~~ ModelDB::Column {
#note "HERE $attr.name() <- $attr.load-filter($v)";
%hash{ $c } = $attr.load-filter($v);
}
else {
%hash{ $c } = $v;
}
}
$c = Capture.new(:%hash, :list(c.list));
#dd $c;
}
nextwith(|$c);
}
method save-id($id) {
my $attr = self.^attributes.first({ .name eq self.HOW.id-column });
return unless $attr;
my $filter-id = $attr.load-filter($id);
$attr.set_value(self, $filter-id);
}
}