1Rose::DB::Object::MakeMUestehrodCso:n:tSrtidb(u3t)ed PerRlosDeo:c:uDmBe:n:tOabtjieocnt::MakeMethods::Std(3)
2
3
4
6 Rose::DB::Object::MakeMethods::Std - Create object methods related to
7 Rose::DB::Object::Std-derived objects.
8
10 package Category;
11 our @ISA = qw(Rose::DB::Object::Std);
12 ...
13
14 package Color;
15 our @ISA = qw(Rose::DB::Object::Std);
16 ...
17
18 package Product;
19 our @ISA = qw(Rose::DB::Object);
20 ...
21
22 use Rose::DB::Object::MakeMethods::Std
23 (
24 object_by_id =>
25 [
26 color => { class => 'Color' },
27
28 category =>
29 {
30 class => 'Category',
31 id_method => 'cat_id',
32 share_db => 0,
33 },
34 ],
35 );
36
37 ...
38
39 $prod = Product->new(...);
40
41 $color = $prod->color;
42
43 # $prod->color call is roughly equivalent to:
44 #
45 # $color = Color->new(id => $prod->color_id,
46 # db => $prod->db);
47 # $ret = $color->load;
48 # return $ret unless($ret);
49 # return $color;
50
51 $cat = $prod->category;
52
53 # $prod->category call is roughly equivalent to:
54 #
55 # $cat = Category->new(id => $prod->cat_id);
56 # $ret = $cat->load;
57 # return $ret unless($ret);
58 # return $cat;
59
61 "Rose::DB::Object::MakeMethods::Std" creates methods related to
62 Rose::DB::Object::Std-derived objects. It inherits from
63 Rose::Object::MakeMethods. See the Rose::Object::MakeMethods
64 documentation to learn about the interface. The method types provided
65 by this module are described below.
66
67 All method types defined by this module are designed to work with
68 objects that are subclasses of (or otherwise conform to the interface
69 of) Rose::DB::Object. In particular, the object is expected to have a
70 "db" method that returns a Rose::DB-derived object. See the
71 Rose::DB::Object::Std documentation for more details.
72
74 object_by_id
75 Create a get/set methods for a single Rose::DB::Object::Std-derived
76 object loaded based on a primary key stored in an attribute of the
77 current object.
78
79 Options
80 "class"
81 The name of the Rose::DB::Object::Std-derived class of the
82 object to be loaded. This option is required.
83
84 "hash_key"
85 The key inside the hash-based object to use for the storage
86 of the object. Defaults to the name of the method.
87
88 "id_method"
89 The name of the method that contains the primary key of the
90 object to be loaded. Defaults to the method name
91 concatenated with "_id".
92
93 "interface"
94 Choose the interface. The only current interface is
95 "get_set", which is the default.
96
97 "share_db"
98 If true, the "db" attribute of the current object is shared
99 with the object loaded. Defaults to true.
100
101 Interfaces
102 "get_set"
103 Creates a method that will attempt to create and load a
104 Rose::DB::Object::Std-derived object based on a primary key
105 stored in an attribute of the current object.
106
107 If passed a single argument of undef, the "hash_key" used
108 to store the object is set to undef. Otherwise, the
109 argument is assumed to be an object of type "class" and is
110 assigned to "hash_key" after having its primary key set to
111 the corresponding value in the current object.
112
113 If called with no arguments and the "hash_key" used to
114 store the object is defined, the object is returned.
115 Otherwise, the object is created and loaded.
116
117 The load may fail for several reasons. The load will not
118 even be attempted if the primary key attribute in the
119 current object is undefined. Instead, undef will be
120 returned. If the call to the newly created object's "load"
121 method returns false, that false value is returned.
122
123 If the load succeeds, the object is returned.
124
125 Example:
126
127 package Category;
128 our @ISA = qw(Rose::DB::Object::Std);
129 ...
130
131 package Color;
132 our @ISA = qw(Rose::DB::Object::Std);
133 ...
134
135 package Product;
136 our @ISA = qw(Rose::DB::Object);
137 ...
138
139 use Rose::DB::Object::MakeMethods::Std
140 (
141 object_by_id =>
142 [
143 color => { class => 'Color' },
144
145 category =>
146 {
147 class => 'Category',
148 id_method => 'cat_id',
149 share_db => 0,
150 },
151 ],
152 );
153
154 ...
155
156 $prod = Product->new(...);
157
158 $color = $prod->color;
159
160 # $prod->color call is roughly equivalent to:
161 #
162 # $color = Color->new(id => $prod->color_id,
163 # db => $prod->db);
164 # $ret = $color->load;
165 # return $ret unless($ret);
166 # return $color;
167
168 $cat = $prod->category;
169
170 # $prod->category call is roughly equivalent to:
171 #
172 # $cat = Category->new(id => $prod->cat_id);
173 # $ret = $cat->load;
174 # return $ret unless($ret);
175 # return $cat;
176
178 John C. Siracusa (siracusa@gmail.com)
179
181 Copyright (c) 2010 by John C. Siracusa. All rights reserved. This
182 program is free software; you can redistribute it and/or modify it
183 under the same terms as Perl itself.
184
185
186
187perl v5.34.0 2022-01-R2o1se::DB::Object::MakeMethods::Std(3)