Commit 8a9d046c authored by Alexander Butenko's avatar Alexander Butenko

Doc fixes

parent f1af21cd
...@@ -2,8 +2,10 @@ dbObject - model implementation on top of the MysqliDb ...@@ -2,8 +2,10 @@ dbObject - model implementation on top of the MysqliDb
Please note, that this library is not pretending to be a full stack ORM but a simple OOP wrapper for mysqlidb Please note, that this library is not pretending to be a full stack ORM but a simple OOP wrapper for mysqlidb
<hr> <hr>
**[Initialization] (#initialization)** ###Initialization
1. Include mysqlidb and dbObject classes. 1. Include mysqlidb and dbObject classes.
2. If you want to use model autoloading instead of manually including them in the scripts use autoload () method. 2. If you want to use model autoloading instead of manually including them in the scripts use autoload () method.
```php ```php
...@@ -14,9 +16,10 @@ require_once ("libs/dbObject.php"); ...@@ -14,9 +16,10 @@ require_once ("libs/dbObject.php");
$db = new Mysqlidb ('localhost', 'user', '', 'testdb'); $db = new Mysqlidb ('localhost', 'user', '', 'testdb');
// enable class autoloading // enable class autoloading
dbObject::autoload ("models"); dbObject::autoload ("models");
``` ```
2. Create simple user class (models/user.php):
3. Create simple user class (models/user.php):
```php ```php
class user extends dbObject { class user extends dbObject {
protected $dbTable = "users"; protected $dbTable = "users";
...@@ -29,7 +32,7 @@ class user extends dbObject { ...@@ -29,7 +32,7 @@ class user extends dbObject {
); );
} }
``` ```
**[Insert Row]** ###Insert Row
1. OOP Way. Just create new object of a needed class, fill it in and call save () method. Save will return 1. OOP Way. Just create new object of a needed class, fill it in and call save () method. Save will return
record id in case of success and false in case if insert will fail. record id in case of success and false in case if insert will fail.
```php ```php
...@@ -40,6 +43,7 @@ $id = $user->save (); ...@@ -40,6 +43,7 @@ $id = $user->save ();
if ($id) if ($id)
echo "user created with id = " . $id; echo "user created with id = " . $id;
``` ```
2. Using arrays 2. Using arrays
```php ```php
$data = Array ('login' => 'demo', $data = Array ('login' => 'demo',
...@@ -53,7 +57,8 @@ if ($id == null) { ...@@ -53,7 +57,8 @@ if ($id == null) {
echo "user created with id = " . $id; echo "user created with id = " . $id;
``` ```
2. Multisave 3. Multisave
```php ```php
$user = new user; $user = new user;
$user->login = 'demo'; $user->login = 'demo';
...@@ -68,12 +73,15 @@ $p->save (); ...@@ -68,12 +73,15 @@ $p->save ();
After save() is call both new objects (user and product) will be saved. After save() is call both new objects (user and product) will be saved.
**[Selects]** ###Selects
Retrieving objects from the database is pretty much the same process of a get ()/getOne () execution without a need to specify table name. Retrieving objects from the database is pretty much the same process of a get ()/getOne () execution without a need to specify table name.
All mysqlidb functions like where(), orWhere(), orderBy(), join etc are supported. All mysqlidb functions like where(), orWhere(), orderBy(), join etc are supported.
Please note that objects returned with join() will not save changes to a joined properties. For this you can use relationships. Please note that objects returned with join() will not save changes to a joined properties. For this you can use relationships.
Select row by primary key Select row by primary key
```php ```php
$user = user::byId (1); $user = user::byId (1);
echo $user->login; echo $user->login;
...@@ -93,7 +101,7 @@ $users = user::where ("login", "demo")->get (Array (10, 20)); ...@@ -93,7 +101,7 @@ $users = user::where ("login", "demo")->get (Array (10, 20));
foreach (users as $u) ... foreach (users as $u) ...
``` ```
**[Update]** ###Update
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. 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.
```php ```php
...@@ -107,14 +115,14 @@ $user = user::byId (1); ...@@ -107,14 +115,14 @@ $user = user::byId (1);
$user->save ($data); $user->save ($data);
``` ```
**[Delete]** ###Delete
Use delete() method on any loaded object. Use delete() method on any loaded object.
```php ```php
$user = user::byId (1); $user = user::byId (1);
$user->delete (); $user->delete ();
``` ```
**[Relations]** ###Relations
Currently dbObject supports only hasMany and hasOne relations only. To use them declare $relations array in the model class like: Currently dbObject supports only hasMany and hasOne relations only. To use them declare $relations array in the model class like:
```php ```php
protected $relations = Array ( protected $relations = Array (
...@@ -125,20 +133,18 @@ Currently dbObject supports only hasMany and hasOne relations only. To use them ...@@ -125,20 +133,18 @@ Currently dbObject supports only hasMany and hasOne relations only. To use them
After that you can get related object via variables and do their modification/removal/display: After that you can get related object via variables and do their modification/removal/display:
```php ```php
$user = user::byId (1); $user = user::byId (1);
// sql: select * from $persontable where id = $personValue // sql: select from $persontable where id = $personValue
echo $user->person->firstName . " " . $user->person->lastName . " have the following products:\n"; echo $user->person->firstName . " " . $user->person->lastName . " have the following products:\n";
// sql: select * from $product_table where userid = $userPrimaryKey // sql: select from $product_table where userid = $userPrimaryKey
foreach ($user->products as $p) { foreach ($user->products as $p) {
echo $p->title; echo $p->title;
} }
``` ```
**[Error checking]** ###Error checking
TBD TBD
**[Validation]** ###Validation
TBD TBD
**[Array as return values]** ###Array as return values
TBD TBD
**[2array and 2json]** ###2array and 2json
TBD TBD
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment