Skip to content

Commit 23f6f77

Browse files
committed
Merge pull request #347 from JustinEldracher/master
Made a few changes to the documentation
2 parents 1e79753 + 2e6ea96 commit 23f6f77

File tree

1 file changed

+91
-91
lines changed

1 file changed

+91
-91
lines changed

dbObject.md

Lines changed: 91 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,66 @@
1-
dbObject - model implementation on top of the MysqliDb
2-
Please note, that this library is not pretending to be a full stack ORM but a simple OOP wrapper for mysqlidb
1+
dbObject - model implementation on top of the MysqliDb.
2+
3+
Please note that this library is not pretending to be a full stack ORM, but simply an OOP wrapper for `mysqlidb`.
34

45
<hr>
56
###Initialization
67

7-
Include mysqlidb and dbObject classes. If you want to use model autoloading instead of manually including them in the scripts use autoload () method.
8+
Include mysqlidb and dbObject classes. If you want to use model autoloading instead of manually including them in the scripts use `autoload()` method.
89
```php
9-
require_once ("libs/MysqliDb.php");
10-
require_once ("libs/dbObject.php");
10+
require_once("libs/MysqliDb.php");
11+
require_once("libs/dbObject.php");
1112

1213
// db instance
13-
$db = new Mysqlidb ('localhost', 'user', '', 'testdb');
14+
$db = new Mysqlidb('localhost', 'user', '', 'testdb');
1415
// enable class autoloading
15-
dbObject::autoload ("models");
16+
dbObject::autoload("models");
1617
```
1718

18-
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+
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.
1920
```php
20-
$user = dbObject::table ("users");
21+
$user = dbObject::table("users");
2122
```
2223

23-
Otherwise basic model should be declared as (in case if autoload is set to 'models' directory filename should be models/user.php):
24+
Otherwise basic model should be declared as:
2425
```php
2526
class user extends dbObject {}
2627
```
28+
In case autoload is set to 'models' directory, the filename should be models/user.php
2729

28-
Class will be related to 'user' table. To change table name define correct name in the $dbTable variable:
30+
Class will be related to 'user' table. To change the table name, define correct name in the `$dbTable` variable:
2931

3032
```php
3133
protected $dbTable = "users";
3234
```
3335

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+
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
37+
will not be working with an objects created with `table()` method.
3638

3739

3840
###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.
41+
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.
4042

4143
##Retrieving All Records
4244

4345
```php
44-
//$users = dbObject::table('users')->get ();
45-
$users = user::get ();
46+
//$users = dbObject::table('users')->get();
47+
$users = user::get();
4648
foreach (users as $u) {
4749
echo $u->login;
4850
}
4951
```
5052

5153
## Using Where Condition And A Limit
5254
```php
53-
$users = user::where ("login", "demo")->get (Array (10, 20));
55+
$users = user::where("login", "demo")->get(Array (10, 20));
5456
foreach (users as $u) ...
5557
```
5658

5759
##Retrieving A Model By Primary Key
5860

5961
```php
60-
//$user = dbObject::table('users')->byId (1);
61-
$user = user::byId (1);
62+
//$user = dbObject::table('users')->byId(1);
63+
$user = user::byId(1);
6264
echo $user->login;
6365
```
6466

@@ -70,26 +72,26 @@ dbObject will also assume that each table has a primary key column named "id". Y
7072

7173

7274
###Insert Row
73-
1. OOP Way. Just create new object of a needed class, fill it in and call save () method. Save will return
75+
1. OOP Way. Just create new object of a needed class, fill it in and call `save()` method. Save will return
7476
record id in case of success and false in case if insert will fail.
7577
```php
7678
//$user = dbObject::table('users');
7779
$user = new user;
7880
$user->login = 'demo';
7981
$user->password = 'demo';
80-
$id = $user->save ();
82+
$id = $user->save();
8183
if ($id)
8284
echo "user created with id = " . $id;
8385
```
8486

8587
2. Using arrays
8688
```php
87-
$data = Array ('login' => 'demo',
89+
$data = Array('login' => 'demo',
8890
'password' => 'demo');
8991
$user = new user ($data);
90-
$id = $user->save ();
92+
$id = $user->save();
9193
if ($id == null) {
92-
print_r ($user->errors);
94+
print_r($user->errors);
9395
echo $db->getLastError;
9496
} else
9597
echo "user created with id = " . $id;
@@ -106,71 +108,71 @@ $p = new product;
106108
$p->title = "Apples";
107109
$p->price = 0.5;
108110
$p->seller = $user;
109-
$p->save ();
111+
$p->save();
110112
```
111113

112-
After save() is called both new objects (user and product) will be saved.
114+
After `save()` is called, both new objects (user and product) will be saved.
113115

114116

115117
###Update
116-
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.
118+
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.
117119

118120
```php
119-
$user = user::byId (1);
121+
$user = user::byId(1);
120122
$user->password = 'demo2';
121-
$user->save ();
123+
$user->save();
122124
```
123125
```php
124-
$data = Array ('password', 'demo2');
125-
$user = user::byId (1);
126-
$user->save ($data);
126+
$data = Array('password', 'demo2');
127+
$user = user::byId(1);
128+
$user->save($data);
127129
```
128130

129131
###Delete
130-
Use delete() method on any loaded object.
132+
Use `delete()` method on any loaded object.
131133
```php
132-
$user = user::byId (1);
133-
$user->delete ();
134+
$user = user::byId(1);
135+
$user->delete();
134136
```
135137

136138
###Relations
137-
Currently dbObject supports only hasMany and hasOne relations. To use them declare $relations array in the model class.
139+
Currently dbObject supports only `hasMany` and `hasOne` relations. To use them declare `$relations` array in the model class.
138140
After that you can get related object via variable names defined as keys.
139141

140-
##HasOne example:
142+
##hasOne example:
141143
```php
142-
protected $relations = Array (
143-
'person' => Array ("hasOne", "person", 'id');
144+
protected $relations = Array(
145+
'person' => Array("hasOne", "person", 'id');
144146
);
145147

146148
...
147149

148-
$user = user::byId (1);
150+
$user = user::byId(1);
149151
// sql: select * from users where id = $personValue
150152
echo $user->person->firstName . " " . $user->person->lastName . " have the following products:\n";
151153
// one more sql: select * from person where id=x
152154
```
153155
Please note, that following way of querying will execute 2 sql queries:
154-
1. select * from users where id=1;
155-
2. select * from person where id=x
156+
1. `select * from users where id=1`
157+
2. `select * from person where id=x`
156158

157-
To optimize this into single select join query use with() method.
159+
To optimize this into single select join query use `with()` method.
158160
```php
159-
$user = user::with ('person')->byId (1);
161+
$user = user::with('person')->byId(1);
160162
// sql: select * from users left join person on person.id = users.id wher id = 1;
161163
echo $user->person->firstName . " " . $user->person->lastName . " have the following products:\n";
162164
```
163165

164-
##HasMany example:
165-
In HasMany Array should be defined target object name (product in example) and a relation key (userid).
166+
##hasMany example:
167+
In the `hasMany` array should be defined the target object name (product in example) and a relation key (userid).
166168
```php
167-
protected $relations = Array (
168-
'products' => Array ("hasMany", "product", 'userid')
169+
protected $relations = Array(
170+
'products' => Array("hasMany", "product", 'userid')
169171
);
170172

171173
...
172174

173-
$user = user::byId (1);
175+
$user = user::byId(1);
174176
// sql: select * from $product_table where userid = $userPrimaryKey
175177
foreach ($user->products as $p) {
176178
echo $p->title;
@@ -179,110 +181,108 @@ In HasMany Array should be defined target object name (product in example) and a
179181

180182
### Joining tables
181183
```php
182-
$depts = product::join ('user');
183-
$depts = product::join ('user', 'productid');
184+
$depts = product::join('user');
185+
$depts = product::join('user', 'productid');
184186
```
185187

186-
First parameter will set an object which should be joined. Second paramter will define a key. Default key is $objectName+'Id'
188+
First parameter will set an object which should be joined. Second paramter will define a key. Default key is `$objectName+'Id'`
187189

188190

189-
NOTE: Objects returned with join() will not save changes to a joined properties. For this you can use relationships.
191+
NOTE: Objects returned with `join()` will not save changes to a joined properties. For this you can use relationships.
190192

191193
###Timestamps
192194
Library provides a transparent way to set timestamps of an object creation and its modification:
193-
To enable that define $timestamps array as follows:
195+
To enable that define `$timestamps` array as follows:
194196
```php
195197
protected $timestamps = Array ('createdAt', 'updatedAt');
196198
```
197-
Field names cant be changed.
199+
Field names can't be changed.
198200

199-
### Array Fields
200-
dbObject can automatically handle array type of values. Optionaly you can store arrays in json encoded or in pipe delimeted format.
201-
To enable automatic json serialization of the field define $jsonFields array in your modal:
201+
###Array Fields
202+
dbObject can automatically handle array type of values. Optionaly you can store arrays in json encoded or in pipe delimited format.
203+
To enable automatic json serialization of the field define `$jsonFields` array in your modal:
202204
```php
203-
protected $jsonFields = Array ('operations');
205+
protected $jsonFields = Array('options');
204206
```
205-
To enable pipe delimetered storage of the field define $arrayFields array in your modal:
207+
To enable pipe delimited storage of the field, define `$arrayFields` array in your modal:
206208
```php
207-
protected $arrayFields = Array ('sections');
209+
protected $arrayFields = Array('sections');
208210
```
209-
211+
The following code will now store `'options'` variable as a json string in the database, and will return an array on load.
212+
Same with the `'sections'` variable except that it will be stored in pipe delimited format.
210213
```php
211214
$user = new user;
212215
$user->login = 'admin';
213-
$user->options = Array ('canReadNews', 'canPostNews', 'canDeleteNews');
214-
$user->sections = Array ('news', 'companyNews');
215-
$user->save ();
216+
$user->options = Array('canReadNews', 'canPostNews', 'canDeleteNews');
217+
$user->sections = Array('news', 'companyNews');
218+
$user->save();
216219
...
217-
$user = user::byId (1);
218-
print_r ($user->options);
220+
$user = user::byId(1);
221+
print_r($user->options);
219222
```
220-
Following code will store 'options' variable as a json string in the database and will return back an array on load.
221-
Same with 'sections' variable except that it will be stored in pipe delimetered format.
222-
223223

224224
###Validation and Error checking
225-
Before saving and updating the row dbObject do input validation. In case validation rules are set but their criteria is not met
226-
then save() will return an error with its description. For example:
225+
Before saving and updating the row, dbObject does input validation. In case validation rules are set but their criteria is
226+
not met, then `save()` will return an error with its description. For example:
227227
```php
228228
$id = $user->save();
229229
if (!$id) {
230230
// show all validation errors
231-
print_r ($user->errors);
231+
print_r($user->errors);
232232
echo $db->getLastQuery();
233233
echo $db->getLastError();
234234
}
235235
echo "user were created with id" . $id;
236236
```
237-
Validation rules must be defined in $dbFields array.
237+
Validation rules must be defined in `$dbFields` array.
238238
```php
239-
protected $dbFields = Array (
240-
'login' => Array ('text', 'required'),
241-
'password' => Array ('text'),
242-
'createdAt' => Array ('datetime'),
243-
'updatedAt' => Array ('datetime'),
244-
'custom' => Array ('/^test/'),
239+
protected $dbFields = Array(
240+
'login' => Array('text', 'required'),
241+
'password' => Array('text'),
242+
'createdAt' => Array('datetime'),
243+
'updatedAt' => Array('datetime'),
244+
'custom' => Array('/^test/'),
245245
);
246246
```
247247
First parameter is a field type. Types could be the one of following: text, bool, int, datetime or a custom regexp.
248248
Second parameter is 'required' and its defines that following entry field be always defined.
249249

250-
NOTE: All variables which are not defined in the $dbFields array will be ignored from insert/update statement.
250+
**NOTE:** All variables which are not defined in the `$dbFields` array will be ignored from insert/update statement.
251251

252252
###Using array as a return value
253-
dbObject can return its data as array instead of object. To do that ArrayBuilder() function should be used in the beginning of the call.
253+
dbObject can return its data as array instead of object. To do that, the `ArrayBuilder()` function should be used in the beginning of the call.
254254
```php
255-
$user = user::ArrayBuilder()->byId (1);
255+
$user = user::ArrayBuilder()->byId(1);
256256
echo $user['login'];
257257

258-
$users = user::ArrayBuilder()->orderBy ("id", "desc")->get ();
258+
$users = user::ArrayBuilder()->orderBy("id", "desc")->get();
259259
foreach ($users as $u)
260260
echo $u['login'];
261261
```
262262

263-
Following call will return data only of the called instance without any relations data. Use with() function to include relation data as well.
263+
The following call will return data only of the called instance without any relations data. Use `with()` function to include relation data as well.
264264
```php
265-
$user = user::ArrayBuilder()->with ("product")->byId (1);
265+
$user = user::ArrayBuilder()->with("product")->byId(1);
266266
print_r ($user['products']);
267267
```
268268

269269
###Using json as a return value
270-
Togeather with ArrayBuilder() and ObjectBuilder() dbObject can return result in json format to avoid extra coding
270+
Together with `ArrayBuilder()` and `ObjectBuilder()`, dbObject can also return a result in json format to avoid extra coding.
271271
```php
272-
$userjson = user::JsonBuilder()->with ("product")->byId (1);
272+
$userjson = user::JsonBuilder()->with("product")->byId(1);
273273
```
274274
###Object serialization
275275

276276
Object could be easily converted to a json string or an array.
277277

278278
```php
279-
$user = user::byId (1);
279+
$user = user::byId(1);
280280
// echo will display json representation of an object
281281
echo $user;
282282
// userJson will contain json representation of an object
283-
$userJson = $user->toJson ();
283+
$userJson = $user->toJson();
284284
// userArray will contain array representation of an object
285-
$userArray = $user->toArray ();
285+
$userArray = $user->toArray();
286286
```
287287

288288
###Examples

0 commit comments

Comments
 (0)