1DBIx::Class::FilterColuUmsne(r3)Contributed Perl DocumenDtBaItxi:o:nClass::FilterColumn(3)
2
3
4
6 DBIx::Class::FilterColumn - Automatically convert column data
7
9 In your Schema or DB class add "FilterColumn" to the top of the
10 component list.
11
12 __PACKAGE__->load_components(qw( FilterColumn ... ));
13
14 Set up filters for the columns you want to convert.
15
16 __PACKAGE__->filter_column( money => {
17 filter_to_storage => 'to_pennies',
18 filter_from_storage => 'from_pennies',
19 });
20
21 sub to_pennies { $_[1] * 100 }
22
23 sub from_pennies { $_[1] / 100 }
24
25 1;
26
28 This component is meant to be a more powerful, but less DWIM-y,
29 DBIx::Class::InflateColumn. One of the major issues with said
30 component is that it only works with references. Generally speaking
31 anything that can be done with DBIx::Class::InflateColumn can be done
32 with this component.
33
35 filter_column
36 __PACKAGE__->filter_column( colname => {
37 filter_from_storage => 'method'|\&coderef,
38 filter_to_storage => 'method'|\&coderef,
39 })
40
41 This is the method that you need to call to set up a filtered column.
42 It takes exactly two arguments; the first being the column name the
43 second being a hash reference with "filter_from_storage" and
44 "filter_to_storage" set to either a method name or a code reference. In
45 either case the filter is invoked as:
46
47 $result->$filter_specification ($value_to_filter)
48
49 with $filter_specification being chosen depending on whether the
50 $value_to_filter is being retrieved from or written to permanent
51 storage.
52
53 If a specific directional filter is not specified, the original value
54 will be passed to/from storage unfiltered.
55
56 get_filtered_column
57 $obj->get_filtered_column('colname')
58
59 Returns the filtered value of the column
60
61 set_filtered_column
62 $obj->set_filtered_column(colname => 'new_value')
63
64 Sets the filtered value of the column
65
67 Some databases have restrictions on values that can be passed to
68 boolean columns, and problems can be caused by passing value that perl
69 considers to be false (such as "undef").
70
71 One solution to this is to ensure that the boolean values are set to
72 something that the database can handle - such as numeric zero and one,
73 using code like this:-
74
75 __PACKAGE__->filter_column(
76 my_boolean_column => {
77 filter_to_storage => sub { $_[1] ? 1 : 0 },
78 }
79 );
80
81 In this case the "filter_from_storage" is not required, as just passing
82 the database value through to perl does the right thing.
83
85 Check the list of additional DBIC resources.
86
88 This module is free software copyright by the DBIx::Class (DBIC)
89 authors. You can redistribute it and/or modify it under the same terms
90 as the DBIx::Class library.
91
92
93
94perl v5.32.0 2020-07-28 DBIx::Class::FilterColumn(3)