YamlRepository
The YamlRepository is a YAML-file based alternative to the database based Doctrine Entity repository.
Implementation
Model
Models that shall be managed by a YamlRepository must extend the base model \Cx\Core\Model\Model\Entity\YamlEntity and must be stored in the folder /Model/Entity of the associated component.
Example
The model for the entity Foo would be stored as modules/MyComponent/Model/Entity/Foo.class.php and would consist of the following base code:
Repository
Entity repositories must extend \Cx\Core\Model\Controller\YamlRepository, must be stored in the folder /Model/Repository of the associated component and must follow the naming scheme <Entity>Repository.
Example
The repository for the entity Foo would be stored as modules/MyComponent/Model/Repository/FooRepository.class.php and would consist of the following base code:
Usage
Working with entities from a YamlRepository is almost identical with entities originating from a Doctrine Entity repository.
Initialization
The initialization of a models repository can either be done directly by instantiating the repository or through the Entity Manager.
Direct Repository Instantiation
Repository from Entity Manager
A YamlRepository can also be fetched through the Entity Manager:
Note
This only works after the database layer has been initialized.
Fetching Entities
Analogous to the Doctrine Entity repository, the YamlRepository provides the following methods for fetching entities:
findAll()find($id)findBy($criteria)
Examples
Fetch entity having ID 3
Fetch entities by a specific property
Persisting Entities
New entities are added to the repository through method add():
Note
See Initialization on how to fetch $repo.
Warning
To persist new entities in the file system, the repository must be flushed using the method flush().
Updating Entities
To persist any changes made to the entities of a repository, the repository must be flushed to the file system by calling flush():
Note
See Initialization on how to fetch $repo.
Checking for existing entities
Use method isManaged() to check if an entity is already managed by a repository:
Note
See Initialization on how to fetch $repo.
Removing Entities
Existing entities are removed from the repository through method remove():
Note
See Initialization on how to fetch $repo.
Warning
To permanently remove entities from the file system, the repository must be flushed using the method flush().