@@ -123,28 +123,103 @@ $user->delete ();
123
123
```
124
124
125
125
###Relations
126
- Currently dbObject supports only hasMany and hasOne relations only. To use them declare $relations array in the model class like:
126
+ Currently dbObject supports only hasMany and hasOne relations. To use them declare $relations array in the model class.
127
+ After that you can get related object via variable names defined as keys.
128
+
129
+ HasOne example:
127
130
``` php
128
131
protected $relations = Array (
129
132
'person' => Array ("hasOne", "person", 'id');
130
- 'products' => Array ("hasMany", "product", 'userid')
131
133
);
134
+
135
+ ...
136
+
137
+ $user = user::byId (1);
138
+ // sql: select * from $persontable where id = $personValue
139
+ echo $user->person->firstName . " " . $user->person->lastName . " have the following products:\n";
132
140
```
133
- After that you can get related object via variables and do their modification/removal/display:
141
+
142
+ In HasMany Array should be defined target object name (product in example) and a relation key (userid).
143
+
144
+ HasMany example:
134
145
``` php
146
+ protected $relations = Array (
147
+ 'products' => Array ("hasMany", "product", 'userid')
148
+ );
149
+
150
+ ...
151
+
135
152
$user = user::byId (1);
136
- // sql: select from $persontable where id = $personValue
137
- echo $user->person->firstName . " " . $user->person->lastName . " have the following products:\n";
138
- // sql: select from $product_table where userid = $userPrimaryKey
153
+ // sql: select * from $product_table where userid = $userPrimaryKey
139
154
foreach ($user->products as $p) {
140
155
echo $p->title;
141
156
}
142
157
```
143
- ###Error checking
144
- TBD
145
- ###Validation
146
- TBD
158
+ ###Timestamps
159
+ Library provides a transparent way to set timestamps of an object creation and its modification:
160
+ To enable that define $timestamps array as follows:
161
+ ``` php
162
+ protected $timestamps = Array ('createdAt', 'updatedAt');
163
+ ```
164
+ Field names cant be changed.
165
+
166
+ ###Validation and Error checking
167
+ Before saving and updating the row dbObject do input validation. In case validation rules are set but their criteria is not met
168
+ then save() will return an error with its description. For example:
169
+ ``` php
170
+ $id = $user->save();
171
+ if (!$id) {
172
+ // show all validation errors
173
+ print_r ($user->errors);
174
+ echo $db->getLastQuery();
175
+ echo $db->getLastError();
176
+ }
177
+ echo "user were created with id" . $id;
178
+ ```
179
+ Validation rules must be defined in $dbFields array.
180
+ ``` php
181
+ protected $dbFields = Array (
182
+ 'login' => Array ('text', 'required'),
183
+ 'password' => Array ('text'),
184
+ 'createdAt' => Array ('datetime'),
185
+ 'updatedAt' => Array ('datetime'),
186
+ 'custom' => Array ('/^test/'),
187
+ );
188
+ ```
189
+ First parameter is a field type. Types could be the one of following: text, bool, int, datetime or a custom regexp.
190
+ Second parameter is 'required' and its defines that following entry field be always defined.
191
+
147
192
###Array as return values
148
- TBD
149
- ###2array and 2json
150
- TBD
193
+ dbObject can return its data as array instead of object. To do that ArrayBuilder() function should be used in the beginning of the call.
194
+ ``` php
195
+ $user = user::ArrayBuilder()->byId (1);
196
+ echo $user['login'];
197
+
198
+ $users = user::ArrayBuilder()->orderBy ("id", "desc")->get ();
199
+ foreach ($users as $u)
200
+ echo $u['login'];
201
+ ```
202
+
203
+ Following call will return data only of the called instance without any relations data. Use with() function to include relation data as well.
204
+
205
+ ``` php
206
+ $user = user::ArrayBuilder()->with ("product")->byId (1);
207
+ print_r ($user['products']);
208
+ ```
209
+ ###Object serialization
210
+
211
+ Object could be easily converted to a json string or an array.
212
+
213
+ ``` php
214
+ $user = user::byId (1);
215
+ // echo will display json representation of an object
216
+ echo $user;
217
+ // userJson will contain json representation of an object
218
+ $userJson = $user->toJson ();
219
+ // userArray will contain array representation of an object
220
+ $userArray = $user->toArray ();
221
+ ```
222
+
223
+ ###Examples
224
+
225
+ Please look for a use examples in tests/dbObjectTests.php file and test models inside the tests/models/ directory
0 commit comments