{"id":15646560,"url":"https://github.com/jeremyharris/cakephp-lazyload","last_synced_at":"2025-04-08T03:13:10.841Z","repository":{"id":5769862,"uuid":"53747073","full_name":"jeremyharris/cakephp-lazyload","owner":"jeremyharris","description":"A lazy loader for CakePHP entities.","archived":false,"fork":false,"pushed_at":"2024-01-05T14:38:46.000Z","size":87,"stargazers_count":61,"open_issues_count":4,"forks_count":10,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-31T18:19:47.108Z","etag":null,"topics":["cakephp-orm","lazyload"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jeremyharris.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-03-12T18:31:11.000Z","updated_at":"2023-10-04T17:41:06.000Z","dependencies_parsed_at":"2024-01-05T15:53:42.366Z","dependency_job_id":null,"html_url":"https://github.com/jeremyharris/cakephp-lazyload","commit_stats":{"total_commits":71,"total_committers":6,"mean_commits":"11.833333333333334","dds":0.295774647887324,"last_synced_commit":"568b41cda62ce9a06ed27eaaa97a84c6ef395edb"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremyharris%2Fcakephp-lazyload","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremyharris%2Fcakephp-lazyload/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremyharris%2Fcakephp-lazyload/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremyharris%2Fcakephp-lazyload/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jeremyharris","download_url":"https://codeload.github.com/jeremyharris/cakephp-lazyload/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247767236,"owners_count":20992548,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cakephp-orm","lazyload"],"created_at":"2024-10-03T12:13:16.875Z","updated_at":"2025-04-08T03:13:10.812Z","avatar_url":"https://github.com/jeremyharris.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Packagist](https://img.shields.io/packagist/dt/jeremyharris/cakephp-lazyload.svg)](https://packagist.org/packages/jeremyharris/cakephp-lazyload)\n[![license](https://img.shields.io/github/license/jeremyharris/cakephp-lazyload.svg)]()\n\n# CakePHP ORM LazyLoad Plugin\n\nThis is an association lazy loader for CakePHP ORM entities. It allows you to\nlazily load association data by accessessing the property, without using\n`contain()` (the eager loader).\n\n## Installation\n\nRequirements\n\n- CakePHP ORM (or the full framework) 5.x\n- sloth\n\n`$ composer require jeremyharris/cakephp-lazyload`\n\nFor older versions of the CakePHP ORM, look at older releases of this library.\n\n## Usage\n\nIf you have a base entity, add the trait to get lazy loading across\nall of your entities. Or, attach it to a single entity to only lazy\nload on that entity:\n\n**src/Model/Entity/User.php**\n```php\n\u003c?php\nnamespace App\\Model\\Entity;\n\nuse Cake\\ORM\\Entity;\nuse JeremyHarris\\LazyLoad\\ORM\\LazyLoadEntityTrait;\n\nclass User extends Entity\n{\n    use LazyLoadEntityTrait;\n}\n```\n\nAssociations to the Users table can now be lazily loaded from the entity!\n\n### Example\n\nLet's assume that our base entity has the `LazyLoadEntityTrait` and:\n\n```text\nBrewery hasMany Beers\nProgrammer belongsToMany Beers\n```\n\nWith the lazy loader, all we need is the entity:\n\n```php\n\u003c?php\n// get an entity, don't worry about contain\n$programmer = $this-\u003eProgrammers-\u003eget(1);\n```\n\nWhen accessing an association property (as if the data was eagerly loaded), the\nassociated data is loaded automatically.\n\n```php\n\u003c?php\n// beers is lazy loaded\nforeach ($programmer-\u003ebeers as $beer) {\n    // brewery is lazy loaded onto $beer\n    echo $programmer-\u003ename . ' drinks beer ' .  $beer-\u003ename . ' from ' . $beer-\u003ebrewery-\u003ename;\n}\n```\n\n### Using contain in conjunction with the lazy loader\n\nThe lazy loader will not overwrite results that are generated by the eager\nloader (`contain()`). You can continue to write complex contain conditions and\nstill take advantage of the lazy loader.\n\n```php\n\u003c?php\n$programmer = $this-\u003eProgrammers-\u003eget(1, [\n    'contain' =\u003e [\n        'Beers'\n    ]\n]);\n\n// beers is loaded via the eager loader\n$programmer-\u003ebeers;\n// brewery is lazy loaded onto $beer[0]\n$programmer-\u003ebeers[0]-\u003ebrewery;\n```\n\n## Entity method support\n\nEntities with the lazy loader trait support lazy loading using the different\nproperty access methods provided by the Cake ORM:\n\n- Getters: `$programmer-\u003eget('beers)`, `$programmer-\u003ebeers`\n- Has: `$programmer-\u003ehas('beers)`\n- Unset: `$programmer-\u003eunsetProperty('beers')`\n\nWhen unsetting a property via `Entity::unsetProperty()`, the property will be\nprevented from being lazily loaded in the future for that entity, as it\nrespects the state in the same way a typical Entity would. If you wish to re-hydrate\nan association, you can use `Table::loadInto` as provided by the ORM:\n\n```php\n\u003c?php\n$programmer = $this-\u003eProgrammers-\u003eget(1);\n\n// beers is lazy loaded\n$programmer-\u003ebeers;\n// remove beers from the entity\n$programmer-\u003eunsetProperty('beers');\n// this now returns false\n$programmer-\u003ehas('beers');\n// if we want access to beers again, we can manually load it\n$programmer = $this-\u003eProgrammers-\u003eloadInto($programmer, ['Beers']);\n```\n\n## Testing\n\nSometimes in tests, we create entities that don't necessarily have tables. When\naccessing a property that doesn't exist, the LazyLoad trait will try to load the\ntable in order to get association data, which would throw an error if the table\ndoesn't exist. To prevent this, you can override `_repository()` in your entity:\n\n```php\n\u003c?php\nnamespace App\\Model\\Entity;\n\nuse Cake\\ORM\\Entity;\nuse Exception;\nuse JeremyHarris\\LazyLoad\\ORM\\LazyLoadEntityTrait;\n\nclass User extends Entity\n{\n    use LazyLoadEntityTrait {\n        _repository as _loadRepository;\n    }\n\n    protected function _repository()\n    {\n        try {\n            $repository = $this-\u003e_loadRepository();\n        } catch (Exception $e) {\n            return false;\n        }\n        return $repository;\n    }\n}\n```\n\nBy default, the LazyLoad trait will throw whatever error bubbles up\n`TableRegistry::get()`.\n\n### Plugins\n\nIf testing plugins entities that don't have tables, make sure to override the\n`_repository()` method to return the *plugin's* table.\n\n## Notes\n\n- **Contain:** This is not a replacement for `contain()`, which can write complex queries to dictate\nwhat data to contain. The lazy loader obeys the association's conditions that\nyou set when defining the association on the table, but apart from that it grabs\nall associated data.\n- **Speed:** Lazy loading in this manner isn't necessarily a speed improvement. In fact, it can be a\ndetriment to speed in certain cases, such as looping over entities that lazy load associations within\nthe loop (creates a single SQL query per-item rather than using joins or the ORM). This plugin is\nintended as a helper for bootstrapping projects.\n- **Hydration:** The lazy loader requires that your result set is hydrated in order to\nprovide lazy loading functionality.\n\n\u003e Special thanks to @lorenzo for reviewing the plugin before its initial release!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeremyharris%2Fcakephp-lazyload","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjeremyharris%2Fcakephp-lazyload","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeremyharris%2Fcakephp-lazyload/lists"}