/cakephp-lazyload

A lazy loader for CakePHP entities.

Primary LanguagePHPMIT LicenseMIT

Build Status

CakePHP ORM LazyLoad Plugin

This is an association lazy loader for CakePHP ORM entities. It allows you to lazily load association data by accessessing the property, without using contain() (the eager loader).

Installation

Requirements

  • CakePHP ORM (or the full framework) >= 3.1.x, < 4.0.0
  • sloth

$ composer require jeremyharris/cakephp-lazyload

Usage

Add the trait to the entity you wish to lazily load association data for. Or, attach it to your base entity if you have one to affect all entities:

src/Model/Entity/User.php

<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;
use JeremyHarris\LazyLoad\ORM\LazyLoadEntityTrait;

class User extends Entity
{
    use LazyLoadEntityTrait;
}

Associations to the Users table can now be lazily loaded from the entity!

Examples

Let's assume that our base entity has the LazyLoadEntityTrait and:

Brewery hasMany Beers
Programmer belongsToMany Beers

With the lazy loader, all we need is the entity:

<?php
// get an entity, don't worry about contain
$programmer = $this->Programmers->get(1);

When accessing an association property (as if the data was eagerly loaded), the associated data is loaded automatically.

<?php
// beers is lazy loaded
foreach ($programmer->beers as $beer) {
    // brewery is lazy loaded onto $beer
    echo $programmer->name . ' drinks beer '  $beer->name . ' from ' . $beer->brewery->name;
}

It also works with Entity::has():

<?php
if ($beer->has('brewery')) {
    echo $beer->brewery->name;
}

Contain

The lazy loader will not overwrite results that are generated by the eager loader (contain()). You can continue to write complex contain conditions and still take advantage of the lazy loader.

$programmer = $this->Programmers->get(1, [
    'contain' => [
        'Beers'
    ]
]);

// beers is loaded via the eager loader
$programmer->beers;
// brewery is lazy loaded onto $beer[0]
$programmer->beers[0]->brewery;

Testing

Sometimes in tests, we create entities that don't necessarily have tables. When accessing a property that doesn't exist, the LazyLoad trait will try to load the table in order to get association data, which would throw an error if the table doesn't exist. To prevent this, you can override _repository() in your entity:

<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;
use JeremyHarris\LazyLoad\ORM\LazyLoadEntityTrait;

class User extends Entity
{
    use LazyLoadEntityTrait {
        _repository as _loadRepository;
    }

    protected function _repository()
    {
        try {
            $repository = $this->_loadRepository();
        } catch (Exception $e) {
            return false;
        }
        return $repository;
    }
}

By default, the LazyLoad trait will throw whatever error bubbles up TableRegistry::get().

Plugins

If testing plugins entities that don't have tables, make sure to override the _repository() method to return the plugin's table.

Notes

This is not a replacement for contain(), which can write complex queries to dictate what data to contain. The lazy loader obeys the association's conditions that you set when defining the association on the table, but apart from that it grabs all associated data.

The lazy loader requires that your result set is hydrated in order to provide lazy loading functionality.

Special thanks to @lorenzo for reviewing the plugin before its initial release!