Doctrine2 中的自定义集合 [英] Custom Collection in Doctrine2

查看:21
本文介绍了Doctrine2 中的自定义集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始使用 Doctrine2,我想知道如何/是否可以使用自定义集合类.搜索将我指向 文档的这一部分:

Just starting to work with Doctrine2, and am wondering how/if I can use a custom collection class. Searches point me to this part of the documentation:

集合值持久字段和属性必须根据DoctrineCommonCollectionsCollection 接口定义.应用程序可以使用集合实现类型在实体持久化之前初始化字段或属性.一旦实体成为托管(或分离),后续访问必须通过接口类型.

Collection-valued persistent fields and properties must be defined in terms of the DoctrineCommonCollectionsCollection interface. The collection implementation type may be used by the application to initialize fields or properties before the entity is made persistent. Once the entity becomes managed (or detached), subsequent access must be through the interface type.

虽然我确信这对某人来说非常清楚,但我对此有点模糊.

While I'm sure that's crystal clear to someone, I'm a little fuzzy on it.

如果我设置我的实体来初始化(比如在 __construct() 中)集合变量到一个实现正确接口的类 - Doctrine2 会继续使用那个类作为集合吗?我的理解正确吗?

If I setup my Entity to initialize (say in __construct()) the collection variable to a class that implements the correct interface - will Doctrine2 continue to use that class as the collection? Am I understanding that correctly?

更新:此外,我从各种线程中收集到,延迟加载中使用的占位符对象可能会影响自定义集合的使用方式.

Update: Also, I gather from various threads that the placeholder object used in lazy loading may influence how a custom collection can be used.

推荐答案

让我试着用例子阐明什么是可能的,什么是不可能的,以及什么是计划好的.

Let me try to clarify what is possible, not possible and planned with examples.

手册中的引用基本上意味着您可以拥有以下自定义实现类型:

The quote from the manual basically means you could have the following custom implementation type:

use DoctrineCommonCollectionsCollection;

// MyCollection is the "implementation type"
class MyCollection implements Collection {
    // ... interface implementation

    // This is not on the Collection interface
    public function myCustomMethod() { ... }
}

现在您可以按如下方式使用它:

Now you could use it as follows:

class MyEntity {
    private $items;
    public function __construct() {
        $this->items = new MyCollection;
    }
    // ... accessors/mutators ...
}

$e = new MyEntity;
$e->getItems()->add(new Item);
$e->getItems()->add(new Item);
$e->getItems()->myCustomMethod(); // calling method on implementation type

// $em instanceof EntityManager
$em->persist($e);

// from now on $e->getItems() may only be used through the interface type

换句话说,只要实体是新的(不是管理的、分离的或移除的),您就可以自由使用集合的具体实现类型,即使它不漂亮.如果它不是 NEW,您必须只访问接口类型(最好是类型提示).这意味着实现类型并不重要.当从数据库中检索持久性 MyEntity 实例时,它不会使用 MyCollection(构造函数永远不会被 Doctrine 调用,因为 Doctrine 只重新构造已经存在的/持久对象,它永远不会创建新"对象).并且由于这样的实体是 MANAGED,因此无论如何都必须通过接口类型进行访问.

In other words, as long as an entity is NEW (not MANAGED, DETACHED or REMOVED) you are free to use the concrete implementation type of collections, even if its not pretty. If it is not NEW, you must access only the interface type (and ideally type-hint on it). That means the implementation type does not really matter. When a persistent MyEntity instance is retrieved from the database, it will not use a MyCollection (constructors are not invoked by Doctrine, ever, since Doctrine only reconstitutes already existing/persistent objects, it never creates "new" ones). And since such an entity is MANAGED, access must happen through the interface type anyways.

现在是计划中的内容.拥有自定义集合的更漂亮的方法是也有一个自定义接口类型,比如 IMyCollection 和 MyCollection 作为实现类型.然后,要使其与 Doctrine 2 持久性服务完美配合,您需要实现自定义 PersistentCollection 实现,例如 MyPersistentCollection,如下所示:

Now to what is planned. The more beautiful way to have custom collections is to also have a custom interface type, say IMyCollection and MyCollection as the implementation type. Then, to make it work perfectly with the Doctrine 2 persistence services you would need to implement a custom PersistentCollection implementation, say, MyPersistentCollection which looks like this:

class MyPersistentCollection implements IMyCollection {
    // ...
}

然后你会告诉映射中的 Doctrine 为该集合使用 MyPersistentCollection 包装器(记住,PersistentCollection 包装 一个集合实现类型,实现相同的接口,以便它可以完成所有持久化在委托给底层集合实现类型之前/之后工作).

Then you would tell Doctrine in the mapping to use the MyPersistentCollection wrapper for that collection (remember, a PersistentCollection wraps a collection implementation type, implementing the same interface, so that it can do all the persistence work before/after delegating to the underlying collection implementation type).

因此自定义集合实现将由 3 个部分组成:

So a custom collection implementation would consist of 3 parts:

  1. 接口类型
  2. Implementation type(实现接口类型)
  3. 持久包装类型(实现接口类型)

这不仅可以编写与 Doctrine 2 ORM 无缝协作的自定义集合,还可以只编写自定义的持久包装器类型,例如优化特定集合的延迟加载/初始化行为到特定的应用需求.

This will not only make it possible to write custom collections that work seemlessly with the Doctrine 2 ORM but also to write only a custom persistent wrapper type, for example to optimise the lazy-loading/initialization behavior of a particular collection to specific application needs.

目前还无法做到这一点,但会做到的.这是编写和使用完全自定义集合的唯一真正优雅且功能齐全的方式,这些集合完美地集成到 Doctrine 2 提供的透明持久性方案中.

It is not yet possible to do this but it will be. This is the only really elegant and fully functional way to write and use completely custom collections that integrate flawlessly in the transparent persistence scheme provided by Doctrine 2.

这篇关于Doctrine2 中的自定义集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆