How do you get an associated entity's id witho

2020-03-26 05:06发布

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

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?

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

1条回答
▲ chillily
2楼-- · 2020-03-26 05:20

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

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.

查看更多
登录 后发表回答