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