Alex Rivera | Logout

Getting a "true" object from a proxy object in doctrine2

Asked 2011-12-01T07:14:17.360
46

Doctrine uses proxy objects to represent related objects in order to facilitate lazy loading. This is a really cool feature, but its causing an issue with something I am trying to accomplish.

I have customized my user object so that they are all required to be related to a different object, which I will call city. This relationship is working fine.

I have a form that my user fills out to generate another object, street. Street is also related to the city object. Instead of having my user select the city when they fill out the form, I want to automatically set it before I persist the object to my database.

I tried using $event->setCity($user->getCity()), but since $user->getCity() returns a proxy object, this generates an error. Is there a function I can call from the proxy object to get the real one?

Note: I am aware I can create a custom query with a join to force doctrine to actually load the related object, but since this is the user (using FOSUserBundle) that would be difficult to do properly.

Edit
Report

1 Answer

0

Doctrines lazy loading is very good at its job, and will replace a proxy object with a real one as soon as you try to use it or any of its properties. If you are having issues due to the proxy objects (like I did in my question) you most probably have an error in your model.

That said, you can tell doctrine to pull all the related data by telling it to "hydrate": $query->getResult(Doctrine\ORM\Query::HYDRATE_ARRAY);

answered 2013-04-15T13:26:44.640

Your Answer