Skip to content

Commit

Permalink
doc: added stack & model chapters [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 28, 2015
1 parent 242ff66 commit 9aa415b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
6 changes: 4 additions & 2 deletions doc/menu.texy
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
- [Introduction | default]
- [Stack | stack]
- [Model | model]
- [Repository | repository]
- Mappers
- [Entity | entity]
- [Relationships | relationships]
- [Collection | collection]
- [Repository | repository]
- Mappers
- [Conventions | conventions]
81 changes: 81 additions & 0 deletions doc/model.texy
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
Model
#####

Model is central manager for repositories. Holds references to them and manages their loading. Model requires repository loader. Orm comes with predefined loader for "Nette\DI":http://doc.nette.org/en/dependency-injection component. This loader uses dependency injection container for lazy loading your repositories.

To define model repositories use php doc `@property-read` annotations.

/--php
namespace MyApp;

/**
* @property-read PostsRepository $posts
* @property-read UsersRepository $users
* @property-read TagsRepository $tags
*/
class Orm extends \Nextras\Orm\Model\Model
{
}
\--

Then add Orm extension into your application `config.neon`:

/--neon
extensions:
orm: Nextras\Orm\Bridges\NetteDI\OrmExtension

orm:
model: MyApp\Orm
\--

Now you can easily inject Orm class into your presenters / services / classes and use property access to get needed repositories:

/--php
namespace MyApp;

class MyService
{
/** @var Orm */
private $orm;

public function __construct(Orm $orm)
{
$this->orm = $orm;
}

public function doSomething($postId)
{
$post = $this->orm->posts->getById($postId);
// ...
}
}
\--


-----

Model with simple loader
========================

If you do not use Nette\DI, you can use predefined SimpleRepositoryLoader. This loader requires already instanciated array of repositories. For creating the stack easily, you can use SimpleLoaderFactory. You have to create instances of repositories and mappers on your own.

/--php
$cache = new Nette\Caching\Cache(...);
$connection = new Nextras\Dbal\Connection(...);

$simpleModelFactory = new SimpleModelFactory($cacheStorage, [
'posts' => new MyApp\PostsRepository(
new MyApp\PostsMapper($connection, $cacheStorage)
),
'users' => new MyApp\UsersRepository(
new MyApp\UsersMapper($connection, $cacheStorage)
),
'tags' => new MyApp\TagsRepository(
new MyApp\TagsMapper($connection, $cacheStorage)
),
]);

$model = $simpleModelFactory->create();
\--

Of course, you can create your own repository loader by implementing `Nextras\Orm\Model\IRepositoryLoader` interface.
2 changes: 1 addition & 1 deletion doc/repository.texy
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ $book->publisher = 1; // id of stored publisher in db
$orm->books->persistAndFlush($book);
\--


-------------

Removing
========
Expand Down
12 changes: 12 additions & 0 deletions doc/stack.texy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Stack
#####

Orm is clearly designed to abstract entities from database implementation, therefore it separates your model into three basic layers:
- **Entities**
Entities are data crates, holds your data, validates values and provides some API for entities connection, relationships and traversal.
- **Repositories**
Repositories form a layer which takes care about your entities. Repository require a mapper as a dependency. Repository do not know anything about your storage (e.g. database), only manages entities and provides API for getting, seatching, storing and removing entities.
- **Mappers**
Mappers are the backend of the whole Orm. The take provides interaction with database/database layer. Orm comes with mapper layer which uses "Nextras\Dbal":/dbal library.

Everything is connected in central Model, which manages all repositories.

0 comments on commit 9aa415b

Please sign in to comment.