Eager Loading
It is the Load where all related classes are loaded in the same query.
The ORM, usually through Joins, will bring all related entities.
Example of use
You have an entity List, where she has several entities Item (one to Many), in its entity List there is an attribute with a Collection of Items.
When you run a Find() or some command to bring these objects all their relations are loaded immediately, ie in your List will already be loaded in memory all your Items (in this example).
Therefore, these objects can already be referenced.
In certain cases the Eager loading becomes unnecessary, because not always when loading an entity you wish to have loaded in memory the related entities.
Example of load with Eager loading of Items related to List:
// Usando em conjunto com o find()
$query = $listas->find('all', ['contain' => ['Items']]);
// Como um método query no objeto
$query = $listas->find('all');
$query->contain(['Items']);
Note that more than one relation with contain can be defined
Lazy Loading
as its name says, it is a lazy load, when you perform a query by a certain entity your relations are not loaded in memory by the initial query, however, when executing some method that calls these records, another query will be executed to complete these related entities.
Example
Following the example of Lists and the related entity Item, if you used for example a Getitems() method from List, the ORM would execute a query and upload those entities to you.
Loads in Cakephp
According to Cakephp’s documentation Lazy Loading should be implemented by you, i.e., the ORM will not do this automatically.
Example of use
In this example the relations List x Item are manually loaded into the entity List.
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
class Lista extends Entity
{
protected function _getItems()
{
$items = TableRegistry::get('Items');
return $items->find('all')
->where(['lista_id' => $this->id])
->toArray();
}
}
Documentation Eager X Lazy Cakephp:
http://book.cakephp.org/3.0/en/orm/entities.html#Lazy-loading-Associations
The
Laravel 4
also uses the so-calledEager Loading Constrains
– Wallace Maxters
Is it language or PHP independent? I got confused :P
– Jéf Bueno
@jbueno independent, put PHP only for example.
– Ricardo