如何在不加载关联实体的情况下获取关联实体的id?

如何在不加载关联实体的情况下获取关联实体的id?

问题描述:

考虑具有(单向)manyToOne关联的实体:

Consider an entity with a (unidirectional) manyToOne association:

EntityX:
    type: entity
    table: x
    id:
        id:
            type: integer
            generator: { strategy: AUTO }

    fields:
        type:
            type: string

    manyToOne:
        entityY:
            targetEntity: EntityY

EntityY的任何属性将通过代理类进行延迟加载;然而,在代理加载之前,EntityY的标识符将(必须)可用。在触发EntityY的代理装载器的情况下,获取此标识符的最佳方式是什么?

Any property of EntityY will be lazy loaded via a proxy class; however, EntityY's identifier will be (has to be) available before the proxy load. What is the best way to get at this identifier without triggering EntityY's proxy loader?

编辑:我正在使用Symfony2(2.0.11),所以Doctrine依赖被限制v2.1

I'm using Symfony2 (2.0.11) so the Doctrine dependency is constrained to v2.1

简短的答案:升级到Doctrine 2.2。 EntityY-> getId()将不再触发加载类。

Short answer: Upgrade to Doctrine 2.2. EntityY->getId() will no longer trigger loading of the class.

很久以前我看到一些代码,显示了如何到达UnitOfWork对象并拉这出来找不到它可能尝试搜索Doctrine2用户邮件列表。但是考虑到2.2修复了这个问题,我只会加载,直到我可以升级。

I did see some code a long time ago which showed how to reach into the UnitOfWork object and pull this out. Can't find it. Might try searching the Doctrine2 user mailing list. But given that 2.2 fixes this problem, I would just live with the loading until I could upgrade.