Skip to content

Commit 386276f

Browse files
Improved the documentation
1 parent 1e79753 commit 386276f

File tree

1 file changed

+87
-88
lines changed

1 file changed

+87
-88
lines changed

dbObject.md

Lines changed: 87 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,62 @@ Please note, that this library is not pretending to be a full stack ORM but a si
44
<hr>
55
###Initialization
66

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

1212
// db instance
13-
$db = new Mysqlidb ('localhost', 'user', '', 'testdb');
13+
$db = new Mysqlidb('localhost', 'user', '', 'testdb');
1414
// enable class autoloading
15-
dbObject::autoload ("models");
15+
dbObject::autoload("models");
1616
```
1717

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.
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.
1919
```php
20-
$user = dbObject::table ("users");
20+
$user = dbObject::table("users");
2121
```
2222

23-
Otherwise basic model should be declared as (in case if autoload is set to 'models' directory filename should be models/user.php):
23+
Otherwise basic model should be declared as:
2424
```php
2525
class user extends dbObject {}
2626
```
27+
In case autoload is set to 'models' directory, the filename should be models/user.php
2728

2829
Class will be related to 'user' table. To change table name define correct name in the $dbTable variable:
2930

3031
```php
3132
protected $dbTable = "users";
3233
```
3334

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

3738

3839
###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.
40+
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.
4041

4142
##Retrieving All Records
4243

4344
```php
44-
//$users = dbObject::table('users')->get ();
45-
$users = user::get ();
45+
//$users = dbObject::table('users')->get();
46+
$users = user::get();
4647
foreach (users as $u) {
4748
echo $u->login;
4849
}
4950
```
5051

5152
## Using Where Condition And A Limit
5253
```php
53-
$users = user::where ("login", "demo")->get (Array (10, 20));
54+
$users = user::where("login", "demo")->get(Array (10, 20));
5455
foreach (users as $u) ...
5556
```
5657

5758
##Retrieving A Model By Primary Key
5859

5960
```php
60-
//$user = dbObject::table('users')->byId (1);
61-
$user = user::byId (1);
61+
//$user = dbObject::table('users')->byId(1);
62+
$user = user::byId(1);
6263
echo $user->login;
6364
```
6465

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

7172

7273
###Insert Row
73-
1. OOP Way. Just create new object of a needed class, fill it in and call save () method. Save will return
74+
1. OOP Way. Just create new object of a needed class, fill it in and call `save()` method. Save will return
7475
record id in case of success and false in case if insert will fail.
7576
```php
7677
//$user = dbObject::table('users');
7778
$user = new user;
7879
$user->login = 'demo';
7980
$user->password = 'demo';
80-
$id = $user->save ();
81+
$id = $user->save();
8182
if ($id)
8283
echo "user created with id = " . $id;
8384
```
8485

8586
2. Using arrays
8687
```php
87-
$data = Array ('login' => 'demo',
88+
$data = Array('login' => 'demo',
8889
'password' => 'demo');
8990
$user = new user ($data);
90-
$id = $user->save ();
91+
$id = $user->save();
9192
if ($id == null) {
92-
print_r ($user->errors);
93+
print_r($user->errors);
9394
echo $db->getLastError;
9495
} else
9596
echo "user created with id = " . $id;
@@ -106,71 +107,71 @@ $p = new product;
106107
$p->title = "Apples";
107108
$p->price = 0.5;
108109
$p->seller = $user;
109-
$p->save ();
110+
$p->save();
110111
```
111112

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

114115

115116
###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.
117+
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.
117118

118119
```php
119-
$user = user::byId (1);
120+
$user = user::byId(1);
120121
$user->password = 'demo2';
121-
$user->save ();
122+
$user->save();
122123
```
123124
```php
124-
$data = Array ('password', 'demo2');
125-
$user = user::byId (1);
126-
$user->save ($data);
125+
$data = Array('password', 'demo2');
126+
$user = user::byId(1);
127+
$user->save($data);
127128
```
128129

129130
###Delete
130-
Use delete() method on any loaded object.
131+
Use `delete()` method on any loaded object.
131132
```php
132-
$user = user::byId (1);
133-
$user->delete ();
133+
$user = user::byId(1);
134+
$user->delete();
134135
```
135136

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

140-
##HasOne example:
141+
##hasOne example:
141142
```php
142-
protected $relations = Array (
143-
'person' => Array ("hasOne", "person", 'id');
143+
protected $relations = Array(
144+
'person' => Array("hasOne", "person", 'id');
144145
);
145146

146147
...
147148

148-
$user = user::byId (1);
149+
$user = user::byId(1);
149150
// sql: select * from users where id = $personValue
150151
echo $user->person->firstName . " " . $user->person->lastName . " have the following products:\n";
151152
// one more sql: select * from person where id=x
152153
```
153154
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
155+
1. `select * from users where id=1`
156+
2. `select * from person where id=x`
156157

157-
To optimize this into single select join query use with() method.
158+
To optimize this into single select join query use `with()` method.
158159
```php
159-
$user = user::with ('person')->byId (1);
160+
$user = user::with('person')->byId(1);
160161
// sql: select * from users left join person on person.id = users.id wher id = 1;
161162
echo $user->person->firstName . " " . $user->person->lastName . " have the following products:\n";
162163
```
163164

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

171172
...
172173

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

180181
### Joining tables
181182
```php
182-
$depts = product::join ('user');
183-
$depts = product::join ('user', 'productid');
183+
$depts = product::join('user');
184+
$depts = product::join('user', 'productid');
184185
```
185186

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

188189

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

191192
###Timestamps
192193
Library provides a transparent way to set timestamps of an object creation and its modification:
193-
To enable that define $timestamps array as follows:
194+
To enable that define `$timestamps` array as follows:
194195
```php
195196
protected $timestamps = Array ('createdAt', 'updatedAt');
196197
```
197-
Field names cant be changed.
198+
Field names can't be changed.
198199

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:
200+
###Array Fields
201+
dbObject can automatically handle array type of values. Optionaly you can store arrays in json encoded or in pipe delimited format.
202+
To enable automatic json serialization of the field define `$jsonFields` array in your modal:
202203
```php
203-
protected $jsonFields = Array ('operations');
204+
protected $jsonFields = Array('options');
204205
```
205-
To enable pipe delimetered storage of the field define $arrayFields array in your modal:
206+
To enable pipe delimited storage of the field, define `$arrayFields` array in your modal:
206207
```php
207-
protected $arrayFields = Array ('sections');
208+
protected $arrayFields = Array('sections');
208209
```
209-
210+
The following code will now store `'options'` variable as a json string in the database, and will return an array on load.
211+
Same with the `'sections'` variable except that it will be stored in pipe delimited format.
210212
```php
211213
$user = new user;
212214
$user->login = 'admin';
213-
$user->options = Array ('canReadNews', 'canPostNews', 'canDeleteNews');
214-
$user->sections = Array ('news', 'companyNews');
215-
$user->save ();
215+
$user->options = Array('canReadNews', 'canPostNews', 'canDeleteNews');
216+
$user->sections = Array('news', 'companyNews');
217+
$user->save();
216218
...
217-
$user = user::byId (1);
218-
print_r ($user->options);
219+
$user = user::byId(1);
220+
print_r($user->options);
219221
```
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-
223222

224223
###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:
224+
Before saving and updating the row, dbObject does input validation. In case validation rules are set but their criteria is
225+
not met, then `save()` will return an error with its description. For example:
227226
```php
228227
$id = $user->save();
229228
if (!$id) {
230229
// show all validation errors
231-
print_r ($user->errors);
230+
print_r($user->errors);
232231
echo $db->getLastQuery();
233232
echo $db->getLastError();
234233
}
235234
echo "user were created with id" . $id;
236235
```
237-
Validation rules must be defined in $dbFields array.
236+
Validation rules must be defined in `$dbFields` array.
238237
```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/'),
238+
protected $dbFields = Array(
239+
'login' => Array('text', 'required'),
240+
'password' => Array('text'),
241+
'createdAt' => Array('datetime'),
242+
'updatedAt' => Array('datetime'),
243+
'custom' => Array('/^test/'),
245244
);
246245
```
247246
First parameter is a field type. Types could be the one of following: text, bool, int, datetime or a custom regexp.
248247
Second parameter is 'required' and its defines that following entry field be always defined.
249248

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

252251
###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.
252+
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.
254253
```php
255-
$user = user::ArrayBuilder()->byId (1);
254+
$user = user::ArrayBuilder()->byId(1);
256255
echo $user['login'];
257256

258-
$users = user::ArrayBuilder()->orderBy ("id", "desc")->get ();
257+
$users = user::ArrayBuilder()->orderBy("id", "desc")->get();
259258
foreach ($users as $u)
260259
echo $u['login'];
261260
```
262261

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

269268
###Using json as a return value
270-
Togeather with ArrayBuilder() and ObjectBuilder() dbObject can return result in json format to avoid extra coding
269+
Together with `ArrayBuilder()` and `ObjectBuilder()`, dbObject can also return a result in json format to avoid extra coding.
271270
```php
272-
$userjson = user::JsonBuilder()->with ("product")->byId (1);
271+
$userjson = user::JsonBuilder()->with("product")->byId(1);
273272
```
274273
###Object serialization
275274

276275
Object could be easily converted to a json string or an array.
277276

278277
```php
279-
$user = user::byId (1);
278+
$user = user::byId(1);
280279
// echo will display json representation of an object
281280
echo $user;
282281
// userJson will contain json representation of an object
283-
$userJson = $user->toJson ();
282+
$userJson = $user->toJson();
284283
// userArray will contain array representation of an object
285-
$userArray = $user->toArray ();
284+
$userArray = $user->toArray();
286285
```
287286

288287
###Examples

0 commit comments

Comments
 (0)