1DBIx::Class::Helper::RoUws:e:rOnCCoonlturminbMuDitBseIsdxi:nP:geC(rl3la)sDso:c:uHmeelnpteart:i:oRnow::OnColumnMissing(3)
2
3
4
6 DBIx::Class::Helper::Row::OnColumnMissing - Configurably handle access
7 of missing columns
8
10 package MyApp::Schema::Result::Account;
11
12 use parent 'DBIx::Class::Core';
13
14 __PACKAGE__->load_components(qw(Helper::Row::OnColumnMissing));
15
16 __PACKAGE__->table('Account');
17
18 __PACKAGE__->add_columns(
19 id => {
20 data_type => 'integer',
21 is_auto_increment => 1,
22 },
23 name => {
24 data_type => 'varchar',
25 size => 25,
26 },
27 book => { data_type => 'text' },
28 );
29
30 sub on_column_missing { 'die' }
31
32 1;
33
34 Or with DBIx::Class::Candy:
35
36 package MyApp::Schema::Result::Account;
37
38 use DBIx::Class::Candy -components => ['Helper::Row::OnColumnMissing'];
39
40 table 'Account';
41
42 column id => {
43 data_type => 'integer',
44 is_auto_increment => 1,
45 };
46
47 column amount => {
48 data_type => 'float',
49 keep_storage_value => 1,
50 };
51
52 column book => { data_type => 'text' };
53
54 sub on_column_missing { 'die' }
55
56 1;
57
58 Elsewhere:
59
60 my $row = $rs->search(undef, { columns => [qw( id name )] })->one_row;
61
62 $row->book # dies
63
65 This module is written to handle the odd condition where you have
66 limited the columns retrieved from the database but accidentally access
67 one of the ones not included. It is configurable by tweaking the
68 "on_column_missing" return value.
69
71 You specify the "mode" by returning the "mode" from the
72 "on_column_missing" method. By default the "mode" returned is "warn".
73
74 The predefined modes are:
75
76 "die"
77 Dies with "Column $name has not been loaded".
78
79 "warn"
80 Warns with "Column $name has not been loaded".
81
82 "nothing"
83 Does nothing
84
85 You can predefine more modes by defining methods named
86 "on_column_$mode", and also override the default modes by overriding
87 the corresponding methods. If you need ad-hoc behavior you can return
88 a code reference and that will be called as a method on the object.
89
90 ADVANCED USAGE
91 If for some reason you find that you need to change your "mode" at
92 runtime, you can always replace the "on_column_missing" with an
93 accessor. For example:
94
95 __PACKAGE__->mk_group_accessors(inherited => 'on_column_missing');
96 __PACKAGE__->on_column_missing('warn');
97
98 Elsewhere:
99
100 $row->on_column_missing('die');
101
102 If you are especially crazy you could even do something like this:
103
104 $row->on_column_missing(sub {
105 my ($self, $column) = @_;
106
107 $self
108 ->result_source
109 ->resultset
110 ->search({ id => $self->id })
111 ->get_column($column)
112 ->single
113 });
114
115 Though if you do that I would make it a named mode (maybe "retrieve"?)
116
118 Thanks ZipRecruiter <https://www.ziprecruiter.com> for funding the
119 development of this module.
120
122 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
123
125 This software is copyright (c) 2020 by Arthur Axel "fREW" Schmidt.
126
127 This is free software; you can redistribute it and/or modify it under
128 the same terms as the Perl 5 programming language system itself.
129
130
131
132perl v5.32.1 2D0B2I1x-:0:1C-l2a7ss::Helper::Row::OnColumnMissing(3)