-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: added stack & model chapters [skip ci]
- Loading branch information
Showing
4 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |