AppBundle 的最佳实践以及如何走得更远
AppBundle's Best Practice and how-to go further
根据this page on the documentation website of symfony they explain how to address your application logic and the best practices. The one thing I am missing in this documentation is the way to store and use your entities. There is a small section,它涵盖了实体中注释的使用,但我们现在都在使用它,我希望你也这样做。
到目前为止我的项目有以下实体:
+------------------------------------------------------------------------+
| Filename | Location | Type |
+------------------------------------------------------------------------+
| Account.php | src/AppBundle/Entity | Entity |
| AccountRepository.php | src/AppBundle/Entity/Repositories | Repository |
| News.php | src/AppBundle/Entity | Entity |
| NewsRepository.php | src/AppBundle/Entity/Repositories | Repository |
+------------------------------------------------------------------------+
现在我无法创建 Forum.php 实体,因为我需要 4 个数据库表:
- forum_sections
- forum_categories
- forum_topics
- forum_posts
我是否需要在 src/AppBundle/Entity
中的一个名为 Forum.php 的文件中创建 4 个实体,或者我是否需要执行其他操作以实现存储和使用这些实体的最佳实践方式。似乎 ForumSections.php
和其他三个文件占用了大量 space,而且很丑。
感谢您的阅读,并提前感谢您抽出宝贵时间留下回复。如果我的问题有任何拼写错误,我很抱歉。
是的,您需要为每个实体创建一个新文件。对于整个框架应用,每个文件必须只写一个 class,并且文件名必须与文件中的 class 名称匹配(扩展名除外)。大量 space 是什么意思?我们在 2015 年,计算机可以处理 TB :-)
如果您遵循与现有实体相同的策略,则需要在以下文件中定义四个实体:
src/AppBundle/Entity/ForumCategory.php
src/AppBundle/Entity/ForumPost.php
src/AppBundle/Entity/ForumSection.php
src/AppBundle/Entity/ForumTopic.php
然后,每个实体的命名空间将是namespace AppBundle\Entity;
但是,当您有多个相关实体时,最好在 Entity/
目录中创建一个子目录。因此,您可以执行以下操作:
src/AppBundle/Entity/Forum/Category.php
src/AppBundle/Entity/Forum/Post.php
src/AppBundle/Entity/Forum/Section.php
src/AppBundle/Entity/Forum/Topic.php
使用此布局无需更改代码。您唯一应该更新的是这些实体的名称空间:namespace AppBundle\Entity\Forum;
根据this page on the documentation website of symfony they explain how to address your application logic and the best practices. The one thing I am missing in this documentation is the way to store and use your entities. There is a small section,它涵盖了实体中注释的使用,但我们现在都在使用它,我希望你也这样做。
到目前为止我的项目有以下实体:
+------------------------------------------------------------------------+
| Filename | Location | Type |
+------------------------------------------------------------------------+
| Account.php | src/AppBundle/Entity | Entity |
| AccountRepository.php | src/AppBundle/Entity/Repositories | Repository |
| News.php | src/AppBundle/Entity | Entity |
| NewsRepository.php | src/AppBundle/Entity/Repositories | Repository |
+------------------------------------------------------------------------+
现在我无法创建 Forum.php 实体,因为我需要 4 个数据库表:
- forum_sections
- forum_categories
- forum_topics
- forum_posts
我是否需要在 src/AppBundle/Entity
中的一个名为 Forum.php 的文件中创建 4 个实体,或者我是否需要执行其他操作以实现存储和使用这些实体的最佳实践方式。似乎 ForumSections.php
和其他三个文件占用了大量 space,而且很丑。
感谢您的阅读,并提前感谢您抽出宝贵时间留下回复。如果我的问题有任何拼写错误,我很抱歉。
是的,您需要为每个实体创建一个新文件。对于整个框架应用,每个文件必须只写一个 class,并且文件名必须与文件中的 class 名称匹配(扩展名除外)。大量 space 是什么意思?我们在 2015 年,计算机可以处理 TB :-)
如果您遵循与现有实体相同的策略,则需要在以下文件中定义四个实体:
src/AppBundle/Entity/ForumCategory.php
src/AppBundle/Entity/ForumPost.php
src/AppBundle/Entity/ForumSection.php
src/AppBundle/Entity/ForumTopic.php
然后,每个实体的命名空间将是namespace AppBundle\Entity;
但是,当您有多个相关实体时,最好在 Entity/
目录中创建一个子目录。因此,您可以执行以下操作:
src/AppBundle/Entity/Forum/Category.php
src/AppBundle/Entity/Forum/Post.php
src/AppBundle/Entity/Forum/Section.php
src/AppBundle/Entity/Forum/Topic.php
使用此布局无需更改代码。您唯一应该更新的是这些实体的名称空间:namespace AppBundle\Entity\Forum;