@@ -3,7 +3,8 @@ dbObject - model implementation on top of the MysqliDb.
Please note that this library is not pretending to be a full stack ORM, but simply an OOP wrapper for `mysqlidb`.
<hr>
###Initialization
### Initialization
Include mysqlidb and dbObject classes. If you want to use model autoloading instead of manually including them in the scripts use `autoload()` method.
```php
...
...
@@ -37,10 +38,10 @@ Both objects created throw new class file creation of with `table()` method will
will not be working with an objects created with `table()` method.
###Selects
###Selects
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.
@@ -71,7 +72,7 @@ dbObject will also assume that each table has a primary key column named "id". Y
```
###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
record id in case of success and false in case if insert will fail.
```php
...
...
@@ -114,7 +115,7 @@ $p->save();
After `save()` is called, both new objects (user and product) will be saved.
###Update
###Update
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.
```php
...
...
@@ -128,18 +129,18 @@ $user = user::byId(1);
$user->save($data);
```
###Delete
###Delete
Use `delete()` method on any loaded object.
```php
$user=user::byId(1);
$user->delete();
```
###Relations
###Relations
Currently dbObject supports only `hasMany` and `hasOne` relations. To use them declare `$relations` array in the model class.
After that you can get related object via variable names defined as keys.
##hasOne example:
##hasOne example:
```php
protected$relations=Array(
'person'=>Array("hasOne","person",'id');
...
...
@@ -163,7 +164,7 @@ To optimize this into single select join query use `with()` method.
echo$user->person->firstName." ".$user->person->lastName." have the following products:\n";
```
##hasMany example:
##hasMany example:
In the `hasMany` array should be defined the target object name (product in example) and a relation key (userid).
```php
protected$relations=Array(
...
...
@@ -190,7 +191,7 @@ First parameter will set an object which should be joined. Second paramter will
NOTE: Objects returned with `join()` will not save changes to a joined properties. For this you can use relationships.
###Timestamps
###Timestamps
Library provides a transparent way to set timestamps of an object creation and its modification:
To enable that define `$timestamps` array as follows:
Object could be easily converted to a json string or an array.
...
...
@@ -285,7 +286,7 @@ Object could be easily converted to a json string or an array.
$userArray=$user->toArray();
```
###Pagination
###Pagination
Use paginate() instead of get() to fetch paginated result
```php
$page=1;
...
...
@@ -296,7 +297,7 @@ echo "showing $page out of " . product::$totalPages;
```
###Hidden Fields
###Hidden Fields
Sometimes it's important to block some fields that can be accessed from outside the model class (for example, the user password).
To block the access to certain fields using the `->` operator, you can declare the `$hidden` array into the model class. This array holds column names that can't be accessed with the `->` operator.
Please look for a use examples in <ahref='tests/dbObjectTests.php'>tests file</a> and test models inside the <ahref='tests/models/'>test models</a> directory