Eager Loading
It is Load where all related classes are loaded into the same query.
The ORM, usually through Joins, will bring all related entities.
Usage example
You have a List entity, where it has multiple Item entities, in its List entity there is an attribute with a Collection of Items .
When you run a Find () or some command to bring these objects all your relationships are loaded immediately, ie your List will already be loaded into memory all your Items (in this example).
So these objects can already be referenced.
In some cases Eager loading becomes unnecessary, because when you load an entity, you do not always want to load related entities into memory.
Example of load with Eager loading 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 relationship can be defined with contain
Lazy Loading
As the name itself says, it is a lazy load, when you run a query by a certain Entity your relationships are not loaded into memory by the initial query, however, when you execute some method that calls those records, another query to populate these related entities.
Example
Following the Lists example and the related entity Item , if you used for example a GetItems () method of List , the ORM execute a query and load those entities for you.
Loads in CakePHP
According to the CakePHP documentation Lazy Loading should be implemented by you, ie ORM will not do this automatically.
Usage example
In this example, List x Item relationships are manually loaded into the List entity.
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();
}
}
Eager X Lazy CakePHP Documentation:
link