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.
```php
$user=dbObject::table("users");
```
Otherwise basic model should be declared as (in case if autoload is set to 'models' directory filename should be models/user.php):
```php
classuserextendsdbObject{}
```
Class will be related to 'user' table. To change table name define correct name in the $dbTable variable:
```php
classuserextendsdbObject{
protected$dbTable="users";
protected$primaryKey="id";
protected$dbFields=Array(
'login'=>Array('text','required'),
'password'=>Array('text'),
'createdAt'=>Array('datetime'),
'updatedAt'=>Array('datetime'),
);
```
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
will not be working with an objects created with table() method.
###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.
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.
...
...
@@ -126,7 +137,7 @@ $user->delete ();
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');
...
...
@@ -141,7 +152,7 @@ HasOne example:
In HasMany Array should be defined target object name (product in example) and a relation key (userid).
HasMany example:
##HasMany example:
```php
protected$relations=Array(
'products'=>Array("hasMany","product",'userid')
...
...
@@ -155,6 +166,18 @@ HasMany example:
echo$p->title;
}
```
### Joining tables
```php
$depts=product::join('user');
$depts=product::join('user','productid');
```
First parameter will set an object which should be joined. Second paramter will define a key. Default key is $objectName+'Id'
NOTE: Objects returned with join() will not save changes to a joined properties. For this you can use relationships.
###Timestamps
Library provides a transparent way to set timestamps of an object creation and its modification:
To enable that define $timestamps array as follows: