我应该在Doctrine 2中使用YAML / XML或注释映射吗?

时间:2022-09-15 19:11:34

I use Doctrine 2 as ORM in Symfony framework. Using annotation-based mapping for entities, I would have to write lot of code (setters and getters, mapping information etc.). Using YAML/XML I won't have to write much just the column definition and metadata. In Doctrine documentation, they use mostly annotations in example with few using YAML or XML.

我在Symfony框架中使用Doctrine 2作为ORM。使用基于注释的实体映射,我将不得不编写大量代码(setter和getter,映射信息等)。使用YAML / XML我不必只写很多列定义和元数据。在Doctrine文档中,它们主要使用注释,很少使用YAML或XML。

Should I use YAML/XML or annotations?

我应该使用YAML / XML还是注释?

For annotations, I can find the documentation for every annotation at http://docs.doctrine-project.org/en/latest/reference/annotations-reference.html. I couldn't find the equivalents of annotation in YAML or XML though; how do I convert the annotations to equivalent YAML/XML expressions?

对于注释,我可以在http://docs.doctrine-project.org/en/latest/reference/annotations-reference.html找到每个注释的文档。我找不到YAML或XML中的注释等价物;如何将注释转换为等效的YAML / XML表达式?

3 个解决方案

#1


20  

This is basically down to preference.

这基本上取决于偏好。

This is my view of things:

这是我对事物的看法:

Pros of annotations:

注释的优点:

  • easier to work with, since they are close to what they describe (the properties)
  • 更容易使用,因为它们接近他们所描述的(属性)
  • lots of examples use them, as you have already noticed
  • 正如您已经注意到的,许多示例都使用它们

Pros of yaml/xml

yaml / xml的优点

  • this keeps the domain objects clean, so absolutely nothing from the persistence layer leaks into the domain (even if in the form of a comment)
  • 这使域对象保持干净,所以持久层绝对没有泄漏到域中(即使是以注释的形式)

If you change the db or the domain, you still have to maintain them in either form, so there's no real advantage in any direction from that standpoint.

如果您更改数据库或域,您仍然必须以任何一种形式维护它们,因此从这个角度来看,任何方向都没有真正的优势。

From a performance standpoint, in production you should be using doctrine's caching to cache the mappings, so they are equal here too.

从性能的角度来看,在生产中你应该使用doctrine的缓存来缓存映射,所以它们在这里也是相同的。

Regarding this: "I would have to write lot of code, setters and getters, mapping information":

关于这个:“我将不得不编写很多代码,setter和getter,映射信息”:

Doctrine requires private/protected properties, so you'll still be writing getters and setters. And you'll still be writing the mapping info, just in another place.

Doctrine需要私有/受保护的属性,因此您仍然会编写getter和setter。你仍然会在另一个地方写下地图信息。

Personally, I'd go with annotations, since it's a lot easier to find examples and info if you need them.

就个人而言,我会使用注释,因为如果需要,可以更容易地找到示例和信息。

#2


20  

There is one disadvantage about using Annotations. Since they are defined on the comments (which is weird), you will NOT be able to use precompilers (like Zend Guard) or some bytecode php caching extensions to enhance your code performance (some of them strip the comments). Because annotation is the only specification with this technical impairment, I would NOT recommend using it.

使用Annotations有一个缺点。由于它们是在注释上定义的(这很奇怪),因此您将无法使用预编译器(如Zend Guard)或某些字节码php缓存扩展来增强代码性能(其中一些会删除注释)。由于注释是唯一具有此技术缺陷的规范,因此我不建议使用它。

Also, writing 'functional' stuff on comments is just weird.

此外,在评论上写“功能性”的东西也很奇怪。

XML is much more verbose than YAML, but is also more widely known. Documentation examples for XML is the poorest amongst all the options.

XML比YAML更冗长,但也更广为人知。 XML的文档示例是所有选项中最差的。

YAML is more readable than any of the others, but it also relies on space identation (which some people don't like). Symfony uses YAML by default for its configurations, so many people that use Symfony opt to use YAML for the doctrine mappings -- which means YAML examples are well documented and there are lots of resources out there.

YAML比其他任何一个都更具可读性,但它也依赖于空间标识(有些人不喜欢)。 Symfony默认使用YAML进行配置,因此许多使用Symfony的人选择使用YAML作为学说映射 - 这意味着YAML示例已有详细记录,并且有大量资源。

I personally prefer YAML. I can generate the entities automatically using the doctrine schema tools (meaning I don't have to write the getters/setters). I can also use the precompilers/bytecode cache options out there without worrying about having my comments stripped out.

我个人更喜欢YAML。我可以使用doctrine模式工具自动生成实体(这意味着我不必编写getter / setter)。我也可以使用预编译器/字节码缓存选项,而不必担心我的注释被删除。


Additional information:

附加信息:

If you're using PHP 7.0 or higher, you can't ever set opcache.save_comments = 0 on php.ini if you're using annotations. PHP has a native way to strip comments, which WILL BREAK your application. Guilherme Blanco, one of Doctrine's most active maintainers and the guy who developed the annotation retrieval classes that are used by most projects (Doctrine, Symfony) has spoken out AGAINST comment annotations (if you read the entire thread, you'll see he's very passionate about this). There has also been some extensive discussion on the issues with comment annotations on Reddit, and most people seemed to agree it's a bad idea.

如果您使用的是PHP 7.0或更高版本,则在使用注释时,不能在php.ini上设置opcache.save_comments = 0。 PHP有一种本地方式来删除注释,这将破坏您的应用程序。 Guilherme Blanco是Doctrine最活跃的维护者之一,也是开发大多数项目(Doctrine,Symfony)使用的注释检索类的人,他已经发表了评论注释(如果你读完整个帖子,你会发现他非常热情)对这个)。关于Reddit评论注释的问题也有一些广泛的讨论,大多数人似乎都认为这是一个坏主意。

Despite all of that, Symfony's official best practices book advices people to use Annotations. So, most Symfony bundles will probably use annotations as their metadata format.

尽管如此,Symfony的官方最佳实践书建议人们使用Annotations。因此,大多数Symfony捆绑包可能会使用注释作为其元数据格式。

#3


6  

I think this is a matter of own taste, I prefer the annotations.

我认为这是一个自己的品味问题,我更喜欢注释。

I use annotation because if you want to know anything about a field its above the field, else you have to open a new file and search the rules you need.

我使用注释,因为如果你想知道一个字段在字段上方的任何信息,否则你必须打开一个新文件并搜索你需要的规则。

Best way to figure it out it to create both cases. Then you look what your prefer.

最好的方法来解决它创建两个案例。然后你看看你喜欢什么。

#1


20  

This is basically down to preference.

这基本上取决于偏好。

This is my view of things:

这是我对事物的看法:

Pros of annotations:

注释的优点:

  • easier to work with, since they are close to what they describe (the properties)
  • 更容易使用,因为它们接近他们所描述的(属性)
  • lots of examples use them, as you have already noticed
  • 正如您已经注意到的,许多示例都使用它们

Pros of yaml/xml

yaml / xml的优点

  • this keeps the domain objects clean, so absolutely nothing from the persistence layer leaks into the domain (even if in the form of a comment)
  • 这使域对象保持干净,所以持久层绝对没有泄漏到域中(即使是以注释的形式)

If you change the db or the domain, you still have to maintain them in either form, so there's no real advantage in any direction from that standpoint.

如果您更改数据库或域,您仍然必须以任何一种形式维护它们,因此从这个角度来看,任何方向都没有真正的优势。

From a performance standpoint, in production you should be using doctrine's caching to cache the mappings, so they are equal here too.

从性能的角度来看,在生产中你应该使用doctrine的缓存来缓存映射,所以它们在这里也是相同的。

Regarding this: "I would have to write lot of code, setters and getters, mapping information":

关于这个:“我将不得不编写很多代码,setter和getter,映射信息”:

Doctrine requires private/protected properties, so you'll still be writing getters and setters. And you'll still be writing the mapping info, just in another place.

Doctrine需要私有/受保护的属性,因此您仍然会编写getter和setter。你仍然会在另一个地方写下地图信息。

Personally, I'd go with annotations, since it's a lot easier to find examples and info if you need them.

就个人而言,我会使用注释,因为如果需要,可以更容易地找到示例和信息。

#2


20  

There is one disadvantage about using Annotations. Since they are defined on the comments (which is weird), you will NOT be able to use precompilers (like Zend Guard) or some bytecode php caching extensions to enhance your code performance (some of them strip the comments). Because annotation is the only specification with this technical impairment, I would NOT recommend using it.

使用Annotations有一个缺点。由于它们是在注释上定义的(这很奇怪),因此您将无法使用预编译器(如Zend Guard)或某些字节码php缓存扩展来增强代码性能(其中一些会删除注释)。由于注释是唯一具有此技术缺陷的规范,因此我不建议使用它。

Also, writing 'functional' stuff on comments is just weird.

此外,在评论上写“功能性”的东西也很奇怪。

XML is much more verbose than YAML, but is also more widely known. Documentation examples for XML is the poorest amongst all the options.

XML比YAML更冗长,但也更广为人知。 XML的文档示例是所有选项中最差的。

YAML is more readable than any of the others, but it also relies on space identation (which some people don't like). Symfony uses YAML by default for its configurations, so many people that use Symfony opt to use YAML for the doctrine mappings -- which means YAML examples are well documented and there are lots of resources out there.

YAML比其他任何一个都更具可读性,但它也依赖于空间标识(有些人不喜欢)。 Symfony默认使用YAML进行配置,因此许多使用Symfony的人选择使用YAML作为学说映射 - 这意味着YAML示例已有详细记录,并且有大量资源。

I personally prefer YAML. I can generate the entities automatically using the doctrine schema tools (meaning I don't have to write the getters/setters). I can also use the precompilers/bytecode cache options out there without worrying about having my comments stripped out.

我个人更喜欢YAML。我可以使用doctrine模式工具自动生成实体(这意味着我不必编写getter / setter)。我也可以使用预编译器/字节码缓存选项,而不必担心我的注释被删除。


Additional information:

附加信息:

If you're using PHP 7.0 or higher, you can't ever set opcache.save_comments = 0 on php.ini if you're using annotations. PHP has a native way to strip comments, which WILL BREAK your application. Guilherme Blanco, one of Doctrine's most active maintainers and the guy who developed the annotation retrieval classes that are used by most projects (Doctrine, Symfony) has spoken out AGAINST comment annotations (if you read the entire thread, you'll see he's very passionate about this). There has also been some extensive discussion on the issues with comment annotations on Reddit, and most people seemed to agree it's a bad idea.

如果您使用的是PHP 7.0或更高版本,则在使用注释时,不能在php.ini上设置opcache.save_comments = 0。 PHP有一种本地方式来删除注释,这将破坏您的应用程序。 Guilherme Blanco是Doctrine最活跃的维护者之一,也是开发大多数项目(Doctrine,Symfony)使用的注释检索类的人,他已经发表了评论注释(如果你读完整个帖子,你会发现他非常热情)对这个)。关于Reddit评论注释的问题也有一些广泛的讨论,大多数人似乎都认为这是一个坏主意。

Despite all of that, Symfony's official best practices book advices people to use Annotations. So, most Symfony bundles will probably use annotations as their metadata format.

尽管如此,Symfony的官方最佳实践书建议人们使用Annotations。因此,大多数Symfony捆绑包可能会使用注释作为其元数据格式。

#3


6  

I think this is a matter of own taste, I prefer the annotations.

我认为这是一个自己的品味问题,我更喜欢注释。

I use annotation because if you want to know anything about a field its above the field, else you have to open a new file and search the rules you need.

我使用注释,因为如果你想知道一个字段在字段上方的任何信息,否则你必须打开一个新文件并搜索你需要的规则。

Best way to figure it out it to create both cases. Then you look what your prefer.

最好的方法来解决它创建两个案例。然后你看看你喜欢什么。