Download the PHP package oasis/doctrine-addon without Composer
On this page you can find all versions of the php package oasis/doctrine-addon. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Please rate this library. Is it a good library?
Informations about the package doctrine-addon
oasis/doctrine-addon
Doctrine ORM 扩展组件,提供:
- AutoIdTrait — 简化自增主键声明
- CascadeRemoveTrait — 解决使用数据库
ON DELETE CASCADE时,EntityManager identity map 和 Second Level Cache 中残留脏数据的问题
Installation
AutoIdTrait
use AutoIdTrait 即可获得自增整数主键 $id 和 getId() 方法:
The Cascade Removal Problem
当 ORM 启用 Second Level Cache 并使用数据库 ON DELETE CASCADE 约束时,删除父 entity 后:
- Identity map 脏数据 —
$em->find()仍然返回已被数据库删除的子 entity - 二级缓存脏数据 — 缓存中仍持有已删除 entity 的数据
- 关联集合不一致 — 持有被删除 entity 引用的其他 entity,其集合未被刷新
例如:Team entity 持有 User 集合。删除一个 User 后,访问 Team 的 members 集合仍会拿到已删除的 User 引用,导致异常。
Doctrine 原生方案各有缺陷:
- 手动失效:复杂,关联链越深越容易遗漏
cascade={"remove"}:ORM 逐条发 DELETE 语句,关联复杂时性能极差
CascadeRemoveTrait Solution
通过 Doctrine 生命周期回调,在 PreRemove 阶段递归收集关联实体,在 PostRemove 阶段统一 detach + evict + refresh。实际删除由数据库 ON DELETE CASCADE 完成。
Prerequisites
- Entity 必须启用 Doctrine Second Level Cache(
@ORM\Cache注解) - 强关联实体的数据库外键必须设置
ON DELETE CASCADE
Usage
Key Concepts
| 概念 | 说明 | getCascadeRemoveableEntities() 返回 |
|---|---|---|
| 强关联实体 | 当前实体删除时也应删除的实体 | Category → Article |
| 弱关联实体 | 持有当前实体引用、需刷新缓存的实体 | Article → Tag(通过 getDirtyEntitiesOnInvalidation()) |
What the Trait Does
| 阶段 | 强关联实体 | 弱关联实体 |
|---|---|---|
| PreRemove | 递归收集 | 收集(排除已在强关联列表中的) |
| PostRemove | detach from EM + evict from L2 cache | evict from L2 cache + refresh from DB |
完整的 CMS 示例(Category / Article / Tag)见 ut/Entity/ 目录。
Development
详见 docs/manual/development.md。
License
MIT
All versions of doctrine-addon with dependencies
PHP Build Version
Package Version
The package oasis/doctrine-addon contains the following files
Loading the files please wait ...