Tony Bogdanov
Tony Bogdanov

Reputation: 7686

Doctrine2: How to fetch pure entity, not a proxy?

Is it possible to fetch an already cached entity in Doctrine2 in it's pure form?

Whenever I try to fetch it using find() or event using the QueryBuilder, it results in a Proxy class, which is impossible to properly store and then wakeup in a Session.

I am using the entity as an User identity, which I would like to persist in the Session, but it seems, that the adapter can't really wake up proxies.

Any ideas?

I've tried find() from the repository, the query builder and even refresh() after the entity is fetched with no success.

Oh, and as I know about the EAGER fetch method, I'm not really sure it's usable in this case and how, since there are no relations taking place, and the proxy actually comes from Doctrines cache, or so I believe.

Upvotes: 2

Views: 3366

Answers (1)

K. Norbert
K. Norbert

Reputation: 10674

Find() never returns a proxy object, you have probably made a mistake somewhere.

Find() always returns an object of your entity class, whose relations will be proxy objects. If you want the relations to be not proxies, you will have to eager load them, either with DQL:

SELECT e, r FROM Enity e JOIN e.relation r

Or, by specifying the relation to be always eager loaded in your mapping, yaml example:

oneToMany:
    phonenumbers:
      targetEntity: Phonenumber
      mappedBy: user
      fetch: EAGER

Upvotes: 4

Related Questions