教义2基础;代理、存储库

发布于 2024-11-08 02:21:26 字数 246 浏览 0 评论 0原文

我可以使用 Doctrine2 并且它有效。但我真的不明白我在做什么。

  1. 我不明白什么是代理,我已经从命令行创建了它们,但我真的需要它们吗?

  2. Doctrine2 注释如何工作? Doctrne2 文件每次都会扫描以从实体注释中查找存储库类文件吗?

我想我需要一些资源来理解 ORM 的基本概念。 项目正在运行,但我不太确定它是否按应有的方式运行..

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2024-11-15 02:21:26

好的,首先要了解的是,您的数据库表和关系被“映射”到您的 php“实体”。例如,您可能有一个用户表。然后您将拥有一个代表用户实体的 php 类。此类中包含受保护/私有成员变量,它们表示用户表中的值以及与其他实体的关系。

Doctrine 从映射文件中获取映射信息。这些可能是实体本身,其中映射信息在实体类中表示为注释(注释)。您也可以使用 YAML 或 XML 将映射信息与实体类完全分离。

一旦您配置了实体类和映射信息并准备就绪,Doctrine 就会在幕后生成“代理”类。您不必创建这些,因为您可以配置 D2 以自动为您创建它们。代理类通过继承提供对实体的访问,并且是 Doctrine 2 的必需部分,没有它们,您将无法访问实体数据。

另一件要记住的事情是工作单元。当您从数据库获取实体(例如用户)时,工作单元 (uow) 在内部保存对其的引用。如果您随后对实体进行更改并保留并刷新它,则 uow 会计算从数据库获取的数据与返回的数据之间的差异,并计算完成作业所需的 sql。

一开始使用 D2 可能会令人生畏,因为它是第一个版本的完全重写,并且有许多新的设计模式需要理解,所以如果您现在不确定,请不要担心,继续使用它并它最终会开始点击。

编辑

要回答您的第二个问题,是的 Doctrine 会根据每个请求扫描您的映射信息。这显然会减慢执行速度。但是,在生产中,您可以使用其中一种驱动程序(ApcCache、MemcachCache 等)启用元数据缓存。那么这不会成为问题。还值得指出的是,D2 支持 3 种类型的缓存:元数据、查询(DQL 转换)和结果(数据库结果缓存)。所有这些都可以使用不同的缓存驱动程序进行配置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文