Entity Lazy Loading
The most efficient way to load data into entities is when these data are resquested or modified. This is the lazy loading pattern.
This feature requires prior knowledge of Entity Models and Model Factories
The simplest way to activate the lazy loading is to move the call to Apine\Entity\EntityInterface::load()
to places where there are reading or writing access to your members. Through the Apine\entity\EntityModel::loaded
member of the abstract entity it is possible to know if the mapper has already fetched the entry from the database. So a method to get a name could look like this :
function get_name(){
if($this->loaded==0){
$this->load();
}
return $this->name;
}
It is also a good idea to defer instanciation of any member of type Apine\Entity\EntityModel
. Instead of initializing them into the loader, it is usual to initialize them on access. The following code is an excerpt from a Profile class with a member of type Image which is an Entity Model in a One to One Relationship.
function get_avatar(){
if(is_null($this->avatar)){
$this->picture=new Image($this->_set_field('avatar'));
}
return $this->avatar;
}
In the case of One to Many or Many to Many Relationships, the same applies using a concrete factory. Here the excerpt of a Article class containing a list of tags in a Many to Many Relationship.
function get_tags(){
if(is_null($this->tags)){
$this->tags=TagFactory::create_for_article($this->_get_id());
}
return $this->tags;
}
Lazy loading of data and lazy instantiation of entity member is an efficient way to maximize the performance of large classes and their containing system too. However, there is little to no effect on performances when used on small entities with few fields and no members of type Apine\Entity\EntityModel
and would only add bloat code.
- Introduction
- Getting Started
- Requirements & Installation
- Application Configuration
- Database Connection
- How to use Controllers
- How to use Views
- How to Manage Session
- How to use the api (Coming Soon)
- Utilities
- Entity Manager
- File Manager
- Access and Save File (Coming Soon)
- Image Methods (Coming Soon)
- XML Manager
- XML Class (Coming Soon)
- Parser (Coming Soon)
- Writer (Coming Soon)
- Advanced Topics
- Extended Users
- Routes
- Templating (Coming Soon)
- Translations and Locales