1Rose::DB::Object::Std(3U)ser Contributed Perl DocumentatiRoonse::DB::Object::Std(3)
2
3
4
6 Rose::DB::Object::Std - Standardized object representation of a single
7 row in a database table.
8
10 package Category;
11
12 use base 'Rose::DB::Object::Std';
13
14 __PACKAGE__->meta->setup
15 (
16 table => 'categories',
17
18 columns =>
19 [
20 id => { type => 'int', primary_key => 1 },
21 name => { type => 'varchar', length => 255 },
22 description => { type => 'text' },
23 ],
24
25 unique_key => 'name',
26 );
27
28 ...
29
30 package Product;
31
32 use base 'Rose::DB::Object::Std';
33
34 __PACKAGE__->meta->setup
35 (
36 table => 'products',
37
38 columns =>
39 [
40 id => { type => 'int', primary_key => 1 },
41 name => { type => 'varchar', length => 255 },
42 description => { type => 'text' },
43 category_id => { type => 'int' },
44
45 status =>
46 {
47 type => 'varchar',
48 check_in => [ 'active', 'inactive' ],
49 default => 'inactive',
50 },
51
52 start_date => { type => 'datetime' },
53 end_date => { type => 'datetime' },
54
55 date_created => { type => 'timestamp', default => 'now' },
56 last_modified => { type => 'timestamp', default => 'now' },
57 ],
58
59 unique_key => 'name',
60
61 foreign_keys =>
62 [
63 category =>
64 {
65 class => 'Category',
66 key_columns => { category_id => 'id' },
67 },
68 ],
69 );
70
71 ...
72
73 $product = Product->new(name => 'GameCube',
74 status => 'active',
75 start_date => '11/5/2001',
76 end_date => '12/1/2007',
77 category_id => 5);
78
79 $product->save or die $product->error;
80
81 $id = $product->id; # auto-generated on save
82
83 ...
84
85 $product = Product->new(id => $id);
86 $product->load or die $product->error;
87
88 print $product->category->name;
89
90 $product->end_date->add(days => 45);
91
92 $product->save or die $product->error;
93
94 ...
95
97 Rose::DB::Object::Std is a subclass of Rose::DB::Object that imposes a
98 few more constraints on the tables it fronts. In addition to the
99 constraints described in the Rose::DB::Object documentation, tables
100 fronted by Rose::DB::Object::Std objects must also fulfill the
101 following requirements:
102
103 · The table must have a single primary key column named "id"
104
105 · The value of the "id" column must be auto-generated if absent.
106
107 Different databases provide for auto-generated column values in
108 different ways. Some provide a native "auto-increment" or "serial"
109 data type, others use sequences behind the scenes.
110
111 Rose::DB::Object::Std (in cooperation with Rose::DB and
112 Rose::DB::Object::Std::Metadata) attempts to hide these details from
113 you. All you have to do is omit the value for the primary key
114 entirely. After the object is "save()"ed, you can retrieve the auto-
115 selected primary key by calling the "id()" method.
116
117 You do have to correctly define the "id" column in the database,
118 however. Here are examples of primary key column definitions that
119 provide auto-generated values, one for each of the databases supported
120 by Rose::DB.
121
122 · PostgreSQL
123
124 CREATE TABLE mytable
125 (
126 id SERIAL NOT NULL PRIMARY KEY,
127 ...
128 );
129
130 · MySQL
131
132 CREATE TABLE mytable
133 (
134 id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
135 ...
136 );
137
138 · Informix
139
140 CREATE TABLE mytable
141 (
142 id SERIAL NOT NULL PRIMARY KEY,
143 ...
144 );
145
146 Other data definitions are possible, of course, but the three
147 definitions above are used in the Rose::DB::Object::Std test suite and
148 are therefore guaranteed to work. If you have success with alternative
149 approaches, patches and/or new tests are welcome.
150
151 To achieve much of this functionality, Rose::DB::Object::Std uses
152 Rose::DB::Object::Std::Metadata objects. The "meta()" method will
153 create these form you. You should not need to do anything special if
154 you use the idiomatic approach to defining metadata as shown in the
155 synopsis.
156
158 Only the methods that are overridden are documented here. See the
159 Rose::DB::Object documentation for the rest.
160
161 meta
162 Returns the Rose::DB::Object::Std::Metadata object associated with
163 this class. This object describes the database table whose rows
164 are fronted by this class: the name of the table, its columns,
165 unique keys, foreign keys, etc. See the
166 Rose::DB::Object::Std::Metadata documentation for more information.
167
168 This can be used as both a class method and an object method.
169
171 John C. Siracusa (siracusa@gmail.com)
172
174 Copyright (c) 2010 by John C. Siracusa. All rights reserved. This
175 program is free software; you can redistribute it and/or modify it
176 under the same terms as Perl itself.
177
178
179
180perl v5.28.0 2015-03-18 Rose::DB::Object::Std(3)