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
+91-91Lines changed: 91 additions & 91 deletions
Original file line number
Diff line number
Diff 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`.
3
4
4
5
<hr>
5
6
###Initialization
6
7
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.
8
9
```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");
11
12
12
13
// db instance
13
-
$db = new Mysqlidb('localhost', 'user', '', 'testdb');
14
+
$db = new Mysqlidb('localhost', 'user', '', 'testdb');
14
15
// enable class autoloading
15
-
dbObject::autoload("models");
16
+
dbObject::autoload("models");
16
17
```
17
18
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.
19
20
```php
20
-
$user = dbObject::table("users");
21
+
$user = dbObject::table("users");
21
22
```
22
23
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:
24
25
```php
25
26
class user extends dbObject {}
26
27
```
28
+
In case autoload is set to 'models' directory, the filename should be models/user.php
27
29
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:
29
31
30
32
```php
31
33
protected $dbTable = "users";
32
34
```
33
35
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.
36
38
37
39
38
40
###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.
@@ -70,26 +72,26 @@ dbObject will also assume that each table has a primary key column named "id". Y
70
72
71
73
72
74
###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
74
76
record id in case of success and false in case if insert will fail.
75
77
```php
76
78
//$user = dbObject::table('users');
77
79
$user = new user;
78
80
$user->login = 'demo';
79
81
$user->password = 'demo';
80
-
$id = $user->save();
82
+
$id = $user->save();
81
83
if ($id)
82
84
echo "user created with id = " . $id;
83
85
```
84
86
85
87
2. Using arrays
86
88
```php
87
-
$data = Array('login' => 'demo',
89
+
$data = Array('login' => 'demo',
88
90
'password' => 'demo');
89
91
$user = new user ($data);
90
-
$id = $user->save();
92
+
$id = $user->save();
91
93
if ($id == null) {
92
-
print_r($user->errors);
94
+
print_r($user->errors);
93
95
echo $db->getLastError;
94
96
} else
95
97
echo "user created with id = " . $id;
@@ -106,71 +108,71 @@ $p = new product;
106
108
$p->title = "Apples";
107
109
$p->price = 0.5;
108
110
$p->seller = $user;
109
-
$p->save();
111
+
$p->save();
110
112
```
111
113
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.
113
115
114
116
115
117
###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.
117
119
118
120
```php
119
-
$user = user::byId(1);
121
+
$user = user::byId(1);
120
122
$user->password = 'demo2';
121
-
$user->save();
123
+
$user->save();
122
124
```
123
125
```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);
127
129
```
128
130
129
131
###Delete
130
-
Use delete() method on any loaded object.
132
+
Use `delete()` method on any loaded object.
131
133
```php
132
-
$user = user::byId(1);
133
-
$user->delete();
134
+
$user = user::byId(1);
135
+
$user->delete();
134
136
```
135
137
136
138
###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.
138
140
After that you can get related object via variable names defined as keys.
139
141
140
-
##HasOne example:
142
+
##hasOne example:
141
143
```php
142
-
protected $relations = Array(
143
-
'person' => Array("hasOne", "person", 'id');
144
+
protected $relations = Array(
145
+
'person' => Array("hasOne", "person", 'id');
144
146
);
145
147
146
148
...
147
149
148
-
$user = user::byId(1);
150
+
$user = user::byId(1);
149
151
// sql: select * from users where id = $personValue
150
152
echo $user->person->firstName . " " . $user->person->lastName . " have the following products:\n";
151
153
// one more sql: select * from person where id=x
152
154
```
153
155
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`
156
158
157
-
To optimize this into single select join query use with() method.
159
+
To optimize this into single select join query use `with()` method.
158
160
```php
159
-
$user = user::with('person')->byId(1);
161
+
$user = user::with('person')->byId(1);
160
162
// sql: select * from users left join person on person.id = users.id wher id = 1;
161
163
echo $user->person->firstName . " " . $user->person->lastName . " have the following products:\n";
162
164
```
163
165
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).
0 commit comments