Commit 1151ee70 authored by Philipp Maurer's avatar Philipp Maurer Committed by Alexander Butenko

Add spaces after '#' (#691)

So the Markdown compiler understands the titles.
parent faee21e0
......@@ -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.
##Retrieving All Records
## Retrieving All Records
```php
//$users = dbObject::table('users')->get();
......@@ -56,7 +57,7 @@ $users = user::where("login", "demo")->get(Array (10, 20));
foreach ($users as $u) ...
```
##Retrieving A Model By Primary Key
## Retrieving A Model By Primary Key
```php
//$user = dbObject::table('users')->byId(1);
......@@ -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:
```php
......@@ -198,7 +199,7 @@ protected $timestamps = Array ('createdAt', 'updatedAt');
```
Field names can't be changed.
###Array Fields
### Array Fields
dbObject can automatically handle array type of values. Optionaly you can store arrays in json encoded or in pipe delimited format.
To enable automatic json serialization of the field define `$jsonFields` array in your modal:
```php
......@@ -221,7 +222,7 @@ Same with the `'sections'` variable except that it will be stored in pipe delimi
print_r($user->options);
```
###Validation and Error checking
### Validation and Error checking
Before saving and updating the row, dbObject does input validation. In case validation rules are set but their criteria is
not met, then `save()` will return an error with its description. For example:
```php
......@@ -249,7 +250,7 @@ Second parameter is 'required' and its defines that following entry field be alw
**NOTE:** All variables which are not defined in the `$dbFields` array will be ignored from insert/update statement.
###Using array as a return value
### Using array as a return value
dbObject can return its data as array instead of object. To do that, the `ArrayBuilder()` function should be used in the beginning of the call.
```php
$user = user::ArrayBuilder()->byId(1);
......@@ -266,12 +267,12 @@ The following call will return data only of the called instance without any rela
print_r ($user['products']);
```
###Using json as a return value
### Using json as a return value
Together with `ArrayBuilder()` and `ObjectBuilder()`, dbObject can also return a result in json format to avoid extra coding.
```php
$userjson = user::JsonBuilder()->with("product")->byId(1);
```
###Object serialization
### Object serialization
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.
......@@ -331,6 +332,6 @@ $user->password = "my-new-password";
Won't change the current `password` value.
###Examples
### Examples
Please look for a use examples in <a href='tests/dbObjectTests.php'>tests file</a> and test models inside the <a href='tests/models/'>test models</a> directory
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