|
| 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 |
| 3 | + |
| 4 | +<hr> |
| 5 | +###Initialization |
| 6 | + |
| 7 | +1. Include mysqlidb and dbObject classes. |
| 8 | + |
| 9 | +2. If you want to use model autoloading instead of manually including them in the scripts use autoload () method. |
| 10 | + |
| 11 | +```php |
| 12 | +require_once ("libs/MysqliDb.php"); |
| 13 | +require_once ("libs/dbObject.php"); |
| 14 | + |
| 15 | +// db instance |
| 16 | +$db = new Mysqlidb ('localhost', 'user', '', 'testdb'); |
| 17 | +// enable class autoloading |
| 18 | +dbObject::autoload ("models"); |
| 19 | +``` |
| 20 | + |
| 21 | +3. Create simple user class (models/user.php): |
| 22 | + |
| 23 | +```php |
| 24 | +class user extends dbObject { |
| 25 | + protected $dbTable = "users"; |
| 26 | + protected $primaryKey = "id"; |
| 27 | + protected $dbFields = Array ( |
| 28 | + 'login' => Array ('text', 'required'), |
| 29 | + 'password' => Array ('text'), |
| 30 | + 'createdAt' => Array ('datetime'), |
| 31 | + 'updatedAt' => Array ('datetime'), |
| 32 | + ); |
| 33 | +} |
| 34 | +``` |
| 35 | +###Insert Row |
| 36 | +1. OOP Way. Just create new object of a needed class, fill it in and call save () method. Save will return |
| 37 | +record id in case of success and false in case if insert will fail. |
| 38 | +```php |
| 39 | +$user = new user; |
| 40 | +$user->login = 'demo'; |
| 41 | +$user->password = 'demo'; |
| 42 | +$id = $user->save (); |
| 43 | +if ($id) |
| 44 | + echo "user created with id = " . $id; |
| 45 | +``` |
| 46 | + |
| 47 | +2. Using arrays |
| 48 | +```php |
| 49 | +$data = Array ('login' => 'demo', |
| 50 | + 'password' => 'demo'); |
| 51 | +$user = new user ($data); |
| 52 | +$id = $user->save (); |
| 53 | +if ($id == null) { |
| 54 | + print_r ($user->errors); |
| 55 | + echo $db->getLastError; |
| 56 | +} else |
| 57 | + echo "user created with id = " . $id; |
| 58 | +``` |
| 59 | + |
| 60 | +3. Multisave |
| 61 | + |
| 62 | +```php |
| 63 | +$user = new user; |
| 64 | +$user->login = 'demo'; |
| 65 | +$user->pass = 'demo'; |
| 66 | + |
| 67 | +$p = new product; |
| 68 | +$p->title = "Apples"; |
| 69 | +$p->price = 0.5; |
| 70 | +$p->seller = $user; |
| 71 | +$p->save (); |
| 72 | +``` |
| 73 | + |
| 74 | +After save() is call both new objects (user and product) will be saved. |
| 75 | + |
| 76 | +###Selects |
| 77 | + |
| 78 | +Retrieving objects from the database is pretty much the same process of a get ()/getOne () execution without a need to specify table name. |
| 79 | + |
| 80 | +All mysqlidb functions like where(), orWhere(), orderBy(), join etc are supported. |
| 81 | +Please note that objects returned with join() will not save changes to a joined properties. For this you can use relationships. |
| 82 | + |
| 83 | +Select row by primary key |
| 84 | + |
| 85 | +```php |
| 86 | +$user = user::byId (1); |
| 87 | +echo $user->login; |
| 88 | +``` |
| 89 | + |
| 90 | +Get all users |
| 91 | +```php |
| 92 | +$users = user::orderBy ('id')->get (); |
| 93 | +foreach (users as $u) { |
| 94 | + echo $u->login; |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +Using where with limit |
| 99 | +```php |
| 100 | +$users = user::where ("login", "demo")->get (Array (10, 20)); |
| 101 | +foreach (users as $u) ... |
| 102 | +``` |
| 103 | + |
| 104 | +###Update |
| 105 | +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. |
| 106 | + |
| 107 | +```php |
| 108 | +$user = user::byId (1); |
| 109 | +$user->password = 'demo2'; |
| 110 | +$user->save (); |
| 111 | +``` |
| 112 | +```php |
| 113 | +$data = Array ('password', 'demo2'); |
| 114 | +$user = user::byId (1); |
| 115 | +$user->save ($data); |
| 116 | +``` |
| 117 | + |
| 118 | +###Delete |
| 119 | +Use delete() method on any loaded object. |
| 120 | +```php |
| 121 | +$user = user::byId (1); |
| 122 | +$user->delete (); |
| 123 | +``` |
| 124 | + |
| 125 | +###Relations |
| 126 | +Currently dbObject supports only hasMany and hasOne relations. To use them declare $relations array in the model class. |
| 127 | +After that you can get related object via variable names defined as keys. |
| 128 | + |
| 129 | +HasOne example: |
| 130 | +```php |
| 131 | + protected $relations = Array ( |
| 132 | + 'person' => Array ("hasOne", "person", 'id'); |
| 133 | + ); |
| 134 | + |
| 135 | + ... |
| 136 | + |
| 137 | + $user = user::byId (1); |
| 138 | + // sql: select * from $persontable where id = $personValue |
| 139 | + echo $user->person->firstName . " " . $user->person->lastName . " have the following products:\n"; |
| 140 | +``` |
| 141 | + |
| 142 | +In HasMany Array should be defined target object name (product in example) and a relation key (userid). |
| 143 | + |
| 144 | +HasMany example: |
| 145 | +```php |
| 146 | + protected $relations = Array ( |
| 147 | + 'products' => Array ("hasMany", "product", 'userid') |
| 148 | + ); |
| 149 | + |
| 150 | + ... |
| 151 | + |
| 152 | + $user = user::byId (1); |
| 153 | + // sql: select * from $product_table where userid = $userPrimaryKey |
| 154 | + foreach ($user->products as $p) { |
| 155 | + echo $p->title; |
| 156 | + } |
| 157 | +``` |
| 158 | +###Timestamps |
| 159 | +Library provides a transparent way to set timestamps of an object creation and its modification: |
| 160 | +To enable that define $timestamps array as follows: |
| 161 | +```php |
| 162 | +protected $timestamps = Array ('createdAt', 'updatedAt'); |
| 163 | +``` |
| 164 | +Field names cant be changed. |
| 165 | + |
| 166 | +###Validation and Error checking |
| 167 | +Before saving and updating the row dbObject do input validation. In case validation rules are set but their criteria is not met |
| 168 | +then save() will return an error with its description. For example: |
| 169 | +```php |
| 170 | +$id = $user->save(); |
| 171 | +if (!$id) { |
| 172 | + // show all validation errors |
| 173 | + print_r ($user->errors); |
| 174 | + echo $db->getLastQuery(); |
| 175 | + echo $db->getLastError(); |
| 176 | +} |
| 177 | +echo "user were created with id" . $id; |
| 178 | +``` |
| 179 | +Validation rules must be defined in $dbFields array. |
| 180 | +```php |
| 181 | + protected $dbFields = Array ( |
| 182 | + 'login' => Array ('text', 'required'), |
| 183 | + 'password' => Array ('text'), |
| 184 | + 'createdAt' => Array ('datetime'), |
| 185 | + 'updatedAt' => Array ('datetime'), |
| 186 | + 'custom' => Array ('/^test/'), |
| 187 | + ); |
| 188 | +``` |
| 189 | +First parameter is a field type. Types could be the one of following: text, bool, int, datetime or a custom regexp. |
| 190 | +Second parameter is 'required' and its defines that following entry field be always defined. |
| 191 | + |
| 192 | +###Array as return values |
| 193 | +dbObject can return its data as array instead of object. To do that ArrayBuilder() function should be used in the beginning of the call. |
| 194 | +```php |
| 195 | + $user = user::ArrayBuilder()->byId (1); |
| 196 | + echo $user['login']; |
| 197 | + |
| 198 | + $users = user::ArrayBuilder()->orderBy ("id", "desc")->get (); |
| 199 | + foreach ($users as $u) |
| 200 | + echo $u['login']; |
| 201 | +``` |
| 202 | + |
| 203 | +Following call will return data only of the called instance without any relations data. Use with() function to include relation data as well. |
| 204 | + |
| 205 | +```php |
| 206 | + $user = user::ArrayBuilder()->with ("product")->byId (1); |
| 207 | + print_r ($user['products']); |
| 208 | +``` |
| 209 | +###Object serialization |
| 210 | + |
| 211 | +Object could be easily converted to a json string or an array. |
| 212 | + |
| 213 | +```php |
| 214 | + $user = user::byId (1); |
| 215 | + // echo will display json representation of an object |
| 216 | + echo $user; |
| 217 | + // userJson will contain json representation of an object |
| 218 | + $userJson = $user->toJson (); |
| 219 | + // userArray will contain array representation of an object |
| 220 | + $userArray = $user->toArray (); |
| 221 | +``` |
| 222 | + |
| 223 | +###Examples |
| 224 | + |
| 225 | +Please look for a use examples in tests/dbObjectTests.php file and test models inside the tests/models/ directory |
0 commit comments