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

查看:123
本文介绍了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:


必须使用 Doctrine\Common\\来定义集合值永久字段和属性\\Collections\Collection 界面。应用程序可以使用集合实现类型来在实体持久化之前初始化字段或属性。一旦实体被管理(或分离),后续访问必须通过接口类型。

Collection-valued persistent fields and properties must be defined in terms of the Doctrine\Common\Collections\Collection 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 Doctrine\Common\Collections\Collection;

// 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

$ b $换句话说,只要一个实体是NEW(不管理,DETACHED或REMOVED),你可以自由地使用具体的集合实现类型,即使它不漂亮。如果不是NEW,则只能访问接口类型(最好是类型提示)。这意味着实现类型并不重要。当从数据库检索到一个永久的MyEntity实例时,它不会使用MyCollection(构造函数不被Doctrine调用,因为Doctrine只能重建已经存在的/持久化的对象,它不会创建新的)。而且由于这样一个实体被管理,访问必须通过接口类型进行。

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. 实现类型(实现接口类型)

  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天全站免登陆