You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Each database table could be easily mapped into a dbObject instance. If you do not want to create model for a simple table its object could be simply created with a table() method.
19
+
```php
20
+
$user = dbObject::table ("users");
21
+
```
22
22
23
+
Otherwise basic model should be declared as (in case if autoload is set to 'models' directory filename should be models/user.php):
23
24
```php
24
-
class user extends dbObject {
25
-
protected $dbTable = "users";
26
-
protected $primaryKey = "id";
27
-
protected $dbFields = Array (
28
-
'login' => Array ('text', 'required'),
29
-
'password' => Array ('text'),
30
-
'createdAt' => Array ('datetime'),
31
-
'updatedAt' => Array ('datetime'),
32
-
);
25
+
class user extends dbObject {}
26
+
```
27
+
28
+
Class will be related to 'user' table. To change table name define correct name in the $dbTable variable:
29
+
30
+
```php
31
+
protected $dbTable = "users";
32
+
```
33
+
34
+
Both objects created throw new class file creation of with table() method will have the same set of methods available. Only exception is that relations, validation or custom model methods
35
+
will not be working with an objects created with table() method.
36
+
37
+
38
+
###Selects
39
+
Retrieving objects from the database is pretty much the same process as a mysqliDb get()/getOne() methods without a need to specify table name. All mysqlidb functions like where(), orWhere(), orderBy(), join etc are supported.
To update model properties just set them and call save () method. As well values that needed to by changed could be passed as an array to the save () method.
@@ -116,7 +127,7 @@ $user->save ($data);
116
127
```
117
128
118
129
###Delete
119
-
Use delete() method on any loaded object.
130
+
Use delete() method on any loaded object.
120
131
```php
121
132
$user = user::byId (1);
122
133
$user->delete ();
@@ -126,7 +137,7 @@ $user->delete ();
126
137
Currently dbObject supports only hasMany and hasOne relations. To use them declare $relations array in the model class.
127
138
After that you can get related object via variable names defined as keys.
128
139
129
-
HasOne example:
140
+
##HasOne example:
130
141
```php
131
142
protected $relations = Array (
132
143
'person' => Array ("hasOne", "person", 'id');
@@ -141,7 +152,7 @@ HasOne example:
141
152
142
153
In HasMany Array should be defined target object name (product in example) and a relation key (userid).
Following code will store 'options' variable as a json string in the database and will return back an array on load.
211
+
Same with 'sections' variable except that it will be stored in pipe delimetered format.
212
+
213
+
166
214
###Validation and Error checking
167
215
Before saving and updating the row dbObject do input validation. In case validation rules are set but their criteria is not met
168
216
then save() will return an error with its description. For example:
@@ -189,6 +237,8 @@ Validation rules must be defined in $dbFields array.
189
237
First parameter is a field type. Types could be the one of following: text, bool, int, datetime or a custom regexp.
190
238
Second parameter is 'required' and its defines that following entry field be always defined.
191
239
240
+
NOTE: All variables which are not defined in the $dbFields array will be ignored from insert/update statement.
241
+
192
242
###Array as return values
193
243
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
244
```php
@@ -222,4 +272,4 @@ Object could be easily converted to a json string or an array.
222
272
223
273
###Examples
224
274
225
-
Please look for a use examples in tests/dbObjectTests.php file and test models inside the tests/models/ directory
275
+
Please look for a use examples in <ahref='tests/dbObjectTests.php'>tests file</a> and test models inside the <ahref='tests/models/'>test models</a> directory
0 commit comments