I just finished reading all the Doctrine 2 documentation, I started my own sandbox, I understood most of the principes, but there is still a question and I couldn't find any complete explanation in the doc.
- What are
Proxyclasses? - When should I use them over entities?
As far as I understand, proxy classes add a layer to let you add some other features to your entities, but why use a proxy instead of implementing the methods themselves in the entity class?
Proxy objects are used whenever your query doesn't return all data required to create an entity. Imagine following scenario:
As you can see this query doesn't return
firstnameandlastnameproperties, therefore you cannot createUserobject. Creation of incomplete entity could lead to unexpected errors.That's why Doctrine will create
UserProxyobject that supports lazy loading. When you'll try to accessfirstnameproperty (which is not loaded) it will first load that value from database.You should always write your code as if you didn't use proxy objects at all. They can be treated as internal objects used by Doctrine.
Technically it could be but take a look at some random proxy object's class. It's full of dirty code, ugh. It's nice to have a clean code in your entities.
You're displaying a list of latest 25 articles and you want to display a details of the first one. Each of them contain a large amount of text, so fetching all that data would be a waste of memory. That's why you don't fetch unnecessary data.