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
Copy file name to clipboardExpand all lines: dbObject.md
+20-19Lines changed: 20 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,8 @@ dbObject - model implementation on top of the MysqliDb.
3
3
Please note that this library is not pretending to be a full stack ORM, but simply an OOP wrapper for `mysqlidb`.
4
4
5
5
<hr>
6
-
###Initialization
6
+
7
+
### Initialization
7
8
8
9
Include mysqlidb and dbObject classes. If you want to use model autoloading instead of manually including them in the scripts use `autoload()` method.
9
10
```php
@@ -37,10 +38,10 @@ Both objects created throw new class file creation of with `table()` method will
37
38
will not be working with an objects created with `table()` method.
38
39
39
40
40
-
###Selects
41
+
###Selects
41
42
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.
@@ -71,7 +72,7 @@ dbObject will also assume that each table has a primary key column named "id". Y
71
72
```
72
73
73
74
74
-
###Insert Row
75
+
###Insert Row
75
76
1. OOP Way. Just create new object of a needed class, fill it in and call `save()` method. Save will return
76
77
record id in case of success and false in case if insert will fail.
77
78
```php
@@ -114,7 +115,7 @@ $p->save();
114
115
After `save()` is called, both new objects (user and product) will be saved.
115
116
116
117
117
-
###Update
118
+
###Update
118
119
To update model properties just set them and call `save()` method. Values that need to be changed could be passed as an array to the `save()` method as well.
119
120
120
121
```php
@@ -128,18 +129,18 @@ $user = user::byId(1);
128
129
$user->save($data);
129
130
```
130
131
131
-
###Delete
132
+
###Delete
132
133
Use `delete()` method on any loaded object.
133
134
```php
134
135
$user = user::byId(1);
135
136
$user->delete();
136
137
```
137
138
138
-
###Relations
139
+
###Relations
139
140
Currently dbObject supports only `hasMany` and `hasOne` relations. To use them declare `$relations` array in the model class.
140
141
After that you can get related object via variable names defined as keys.
141
142
142
-
##hasOne example:
143
+
##hasOne example:
143
144
```php
144
145
protected $relations = Array(
145
146
'person' => Array("hasOne", "person", 'id');
@@ -163,7 +164,7 @@ To optimize this into single select join query use `with()` method.
163
164
echo $user->person->firstName . " " . $user->person->lastName . " have the following products:\n";
164
165
```
165
166
166
-
##hasMany example:
167
+
##hasMany example:
167
168
In the `hasMany` array should be defined the target object name (product in example) and a relation key (userid).
168
169
```php
169
170
protected $relations = Array(
@@ -190,15 +191,15 @@ First parameter will set an object which should be joined. Second paramter will
190
191
191
192
NOTE: Objects returned with `join()` will not save changes to a joined properties. For this you can use relationships.
192
193
193
-
###Timestamps
194
+
###Timestamps
194
195
Library provides a transparent way to set timestamps of an object creation and its modification:
195
196
To enable that define `$timestamps` array as follows:
Object could be easily converted to a json string or an array.
277
278
@@ -285,7 +286,7 @@ Object could be easily converted to a json string or an array.
285
286
$userArray = $user->toArray();
286
287
```
287
288
288
-
###Pagination
289
+
###Pagination
289
290
Use paginate() instead of get() to fetch paginated result
290
291
```php
291
292
$page = 1;
@@ -296,7 +297,7 @@ echo "showing $page out of " . product::$totalPages;
296
297
297
298
```
298
299
299
-
###Hidden Fields
300
+
###Hidden Fields
300
301
Sometimes it's important to block some fields that can be accessed from outside the model class (for example, the user password).
301
302
302
303
To block the access to certain fields using the `->` operator, you can declare the `$hidden` array into the model class. This array holds column names that can't be accessed with the `->` operator.
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