Doctrine2 basic; Proxies, Repositories

2019-05-21 10:46发布

I can use Doctrine2 and it works. But i really dont understand what i am doing..

  1. I don't understand what are proxies, i have created em all from command line, but do i really need them ?

  2. How Doctrine2 annotations work? Does Doctrne2 file scan every time to find the repository class file from the Entity annotation ?

i think i need some resources to understand the basic consepts of the ORM.. Project is working but i am not so sure that is working as it should be..

Thanks

2条回答
放我归山
2楼-- · 2019-05-21 11:20

Ok, first thing to understand is that your database tables and relationships are 'mapped' to your php 'entities'. For example, you may have a users table. You will then have a php class that represents the users entity. Inside this class are protected/private member variables that represent values in your users table and relationships to other entities.

Doctrine gets the mapping information from your mapping files. These may be the entities themselves where the mapping information is represented as annotations (comments) in your entity classes. You can alternatively separate the mapping information from your entity classes altogether by using YAML or XML.

Once you have your Entity classes and mapping information configured and ready to go, Doctrine, behind the scenes produces 'Proxy' classes. You don't have to create these as you can configure D2 to create them automatically for you. The proxy classes provide access to your entities via inheritance and are a required part of Doctrine 2, without them, you could not access entity data.

The other thing to remember is the unit of work. When you get an entity from the db, say a user, the unit of work (uow) holds a reference to it internally. If you then make changes to the entity and persist and flush it, the uow calculates the difference between the one it got from the db and the one it is returning and calculate the sql needed to complete the job.

It can be intimidating to work with D2 at first as it is a complete rewrite of the 1st version and there are many new design patterns to understand so don't' worry if you're not sure of it now, keep working with it and it will eventually start clicking.

EDIT

To answer your second question, yes Doctrine scans your mapping information on every request. This will obviously slow down execution. However, in production you would enable metadata caching using one of the drivers (ApcCache, MemcachCache etc). This then does not become an issue. It's also worth pointing out that D2 supports 3 types of caching, metadata, query (DQL conversion) and result (Database result caching). All are configurable with different cache drivers.

查看更多
登录 后发表回答