承接 jeremyharris/cakephp-lazyload 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jeremyharris/cakephp-lazyload

Composer 安装命令:

composer require jeremyharris/cakephp-lazyload

包简介

An association lazy-loader for CakePHP

README 文档

README

Packagist license

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) 5.x
  • sloth

$ composer require jeremyharris/cakephp-lazyload

For older versions of the CakePHP ORM, look at older releases of this library.

Usage

If you have a base entity, add the trait to get lazy loading across all of your entities. Or, attach it to a single entity to only lazy load on that entity:

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!

Example

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;
}

Using contain in conjunction with the lazy loader

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.

<?php
$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;

Entity method support

Entities with the lazy loader trait support lazy loading using the different property access methods provided by the Cake ORM:

  • Getters: $programmer->get('beers), $programmer->beers
  • Has: $programmer->has('beers)
  • Unset: $programmer->unsetProperty('beers')

When unsetting a property via Entity::unsetProperty(), the property will be prevented from being lazily loaded in the future for that entity, as it respects the state in the same way a typical Entity would. If you wish to re-hydrate an association, you can use Table::loadInto as provided by the ORM:

<?php
$programmer = $this->Programmers->get(1);

// beers is lazy loaded
$programmer->beers;
// remove beers from the entity
$programmer->unsetProperty('beers');
// this now returns false
$programmer->has('beers');
// if we want access to beers again, we can manually load it
$programmer = $this->Programmers->loadInto($programmer, ['Beers']);

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 Exception;
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

  • Contain: 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.
  • Speed: Lazy loading in this manner isn't necessarily a speed improvement. In fact, it can be a detriment to speed in certain cases, such as looping over entities that lazy load associations within the loop (creates a single SQL query per-item rather than using joins or the ORM). This plugin is intended as a helper for bootstrapping projects.
  • Hydration: 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!

jeremyharris/cakephp-lazyload 适用场景与选型建议

jeremyharris/cakephp-lazyload 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 615.69k 次下载、GitHub Stars 达 61, 最近一次更新时间为 2016 年 03 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「plugin」 「cakephp」 「lazy load」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jeremyharris/cakephp-lazyload 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jeremyharris/cakephp-lazyload 我们能提供哪些服务?
定制开发 / 二次开发

基于 jeremyharris/cakephp-lazyload 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 615.69k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 61
  • 点击次数: 27
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 61
  • Watchers: 7
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2016-03-14