|
| 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] (#initialization)** |
| 6 | +1. Include mysqlidb and dbObject classes. |
| 7 | +2. If you want to use model autoloading instead of manually including them in the scripts use autoload () method. |
| 8 | + |
| 9 | +```php |
| 10 | +require_once ("libs/MysqliDb.php"); |
| 11 | +require_once ("libs/dbObject.php"); |
| 12 | + |
| 13 | +// db instance |
| 14 | +$db = new Mysqlidb ('localhost', 'user', '', 'testdb'); |
| 15 | +// enable class autoloading |
| 16 | +dbObject::autoload ("models"); |
| 17 | + |
| 18 | +``` |
| 19 | +2. Create simple user class (models/user.php): |
| 20 | +```php |
| 21 | +class user extends dbObject { |
| 22 | + protected $dbTable = "users"; |
| 23 | + protected $primaryKey = "id"; |
| 24 | + protected $dbFields = Array ( |
| 25 | + 'login' => Array ('text', 'required'), |
| 26 | + 'password' => Array ('text'), |
| 27 | + 'createdAt' => Array ('datetime'), |
| 28 | + 'updatedAt' => Array ('datetime'), |
| 29 | + ); |
| 30 | +} |
| 31 | +``` |
| 32 | +**[Insert Row]** |
| 33 | +1. OOP Way. Just create new object of a needed class, fill it in and call save () method. Save will return |
| 34 | +record id in case of success and false in case if insert will fail. |
| 35 | +```php |
| 36 | +$user = new user; |
| 37 | +$user->login = 'demo'; |
| 38 | +$user->password = 'demo'; |
| 39 | +$id = $user->save (); |
| 40 | +if ($id) |
| 41 | + echo "user created with id = " . $id; |
| 42 | +``` |
| 43 | +2. Using arrays |
| 44 | +```php |
| 45 | +$data = Array ('login' => 'demo', |
| 46 | + 'password' => 'demo'); |
| 47 | +$user = new user ($data); |
| 48 | +$id = $user->save (); |
| 49 | +if ($id == null) { |
| 50 | + print_r ($user->errors); |
| 51 | + echo $db->getLastError; |
| 52 | +} else |
| 53 | + echo "user created with id = " . $id; |
| 54 | +``` |
| 55 | + |
| 56 | +2. Multisave |
| 57 | +```php |
| 58 | +$user = new user; |
| 59 | +$user->login = 'demo'; |
| 60 | +$user->pass = 'demo'; |
| 61 | + |
| 62 | +$p = new product; |
| 63 | +$p->title = "Apples"; |
| 64 | +$p->price = 0.5; |
| 65 | +$p->seller = $user; |
| 66 | +$p->save (); |
| 67 | +``` |
| 68 | + |
| 69 | +After save() is call both new objects (user and product) will be saved. |
| 70 | + |
| 71 | +**[Selects]** |
| 72 | +Retrieving objects from the database is pretty much the same process of a get ()/getOne () execution without a need to specify table name. |
| 73 | +All mysqlidb functions like where(), orWhere(), orderBy(), join etc are supported. |
| 74 | +Please note that objects returned with join() will not save changes to a joined properties. For this you can use relationships. |
| 75 | + |
| 76 | +Select row by primary key |
| 77 | +```php |
| 78 | +$user = user::byId (1); |
| 79 | +echo $user->login; |
| 80 | +``` |
| 81 | + |
| 82 | +Get all users |
| 83 | +```php |
| 84 | +$users = user::orderBy ('id')->get (); |
| 85 | +foreach (users as $u) { |
| 86 | + echo $u->login; |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +Using where with limit |
| 91 | +```php |
| 92 | +$users = user::where ("login", "demo")->get (Array (10, 20)); |
| 93 | +foreach (users as $u) ... |
| 94 | +``` |
| 95 | + |
| 96 | +**[Update]** |
| 97 | +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. |
| 98 | + |
| 99 | +```php |
| 100 | +$user = user::byId (1); |
| 101 | +$user->password = 'demo2'; |
| 102 | +$user->save (); |
| 103 | +``` |
| 104 | +```php |
| 105 | +$data = Array ('password', 'demo2'); |
| 106 | +$user = user::byId (1); |
| 107 | +$user->save ($data); |
| 108 | +``` |
| 109 | + |
| 110 | +**[Delete]** |
| 111 | +Use delete() method on any loaded object. |
| 112 | +```php |
| 113 | +$user = user::byId (1); |
| 114 | +$user->delete (); |
| 115 | +``` |
| 116 | + |
| 117 | +**[Relations]** |
| 118 | +Currently dbObject supports only hasMany and hasOne relations only. To use them declare $relations array in the model class like: |
| 119 | +```php |
| 120 | + protected $relations = Array ( |
| 121 | + 'person' => Array ("hasOne", "person", 'id'); |
| 122 | + 'products' => Array ("hasMany", "product", 'userid') |
| 123 | + ); |
| 124 | +``` |
| 125 | +After that you can get related object via variables and do their modification/removal/display: |
| 126 | +```php |
| 127 | + $user = user::byId (1); |
| 128 | + // sql: select * from $persontable where id = $personValue |
| 129 | + echo $user->person->firstName . " " . $user->person->lastName . " have the following products:\n"; |
| 130 | + // sql: select * from $product_table where userid = $userPrimaryKey |
| 131 | + foreach ($user->products as $p) { |
| 132 | + echo $p->title; |
| 133 | + } |
| 134 | +``` |
| 135 | +**[Error checking]** |
| 136 | +TBD |
| 137 | +**[Validation]** |
| 138 | +TBD |
| 139 | +**[Array as return values]** |
| 140 | +TBD |
| 141 | +**[2array and 2json]** |
| 142 | +TBD |
| 143 | + |
| 144 | + |
0 commit comments