-
Notifications
You must be signed in to change notification settings - Fork 3
Entity Repositories
Flextrine stores entities in Entity Repositories. An entity repository can be thought of as a kind of local cache of the database. The main job of an entity repository is to allow Flextrine to keep track of which entities have already been loaded from the database, and make sure that there is only ever a single instance of each entity in existence at one time. The repository can also be used as a cache to avoid constantly querying the database.
Entities can be in one of 4 states - NEW, MANAGED, DETACHED or REMOVED.
-
NEW: A NEW entity is an entity which has been created with the
new
command, but has not yet been persisted. - MANAGED: A MANAGED entity is an entity which exists in a repository. This means it has either been persisted, merged or has been loaded from the server.
- DETACHED: A DETACHED entity is an entity which has been detached using EntityManager::detach(). This means it is an object that was previously MANAGED, but for which you have taken an unmanaged copy.
- REMOVED: A REMOVED entity has been removed with EntityManager::remove() and will be deleted from the database on the next flush.
The state of an entity can be retrieved directly using EntityRepository::getEntityState():
// This will return EntityRepository.NEW, EntityRepository.MANAGED, EntityRepository.DETACHED or EntityRepository.REMOVED
var state:String = em.getRepository(Doctor).getEntityState(myDoctor);
Internally, an entity repository stores entities in an EntityCollection called entities
; this implements ListCollectionView and can therefore be used directly as a source for databinding. However, since v0.9 entities in a repository can become eligible for garbage collection after a certain amount of time has passed so it is no longer advisable to use entities
in this way (unless you set configuration.entityTimeToLive = -1
- see Garbage collection for more details).
There are a number of methods for finding entities that are currently within the repository. These are exactly equivalent to searching through the entities
collection using normal methods and are provided for convenience only. Note that these methods will be affected by the same garbage collection issues as entities
itself.
var doctorRepository:IEntityRepository = em.getRepository(Doctor);
// Get the Doctor with id 3
var doc3:Doctor = doctorRepository.find(3);
// Get 'Doctor Evil'
var docEvil:Doctor = doctorRepository.findOneBy( { name: "Doctor Evil" } );
// Get all the Doctors who are not on holiday
var availableDocs:Array = doctorRepository.findBy( { onHoliday: false } );
// Get all the Doctors
var allDocs:Array = doctorRepository.findAll();
Just to re-iterate, all the find methods just search entities that are already in the repository, and do not make any calls to the server. There are matching load methods which perform the same functions as the find method, but execute on the server and populate the repositories. See Working with objects for more details.