与Doctrine ORM的表命名约定 [英] Table naming convention with Doctrine ORM

查看:113
本文介绍了与Doctrine ORM的表命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Doctrine ORM时,是否有约定表命名?我喜欢用复数命名表,但是如果有一个约定,我想坚持下去。



所以表用户将与使用fk的表相关联单数('user_id')。



是否有最佳做法(使用单数或多个表名),如果是后者,那么这适用于复数并不是添加's'的简单例子。



例如,我目前有一个名为category的表,而不是类别来维护约定添加's'。



这是一个明智的做法吗?

解决方案

p>当我使用自己的基本ORM时,我曾经使用多个表名,但是当我开始使用symfony + Propel和现在的一些Doctrine时,我切换到单个表名。这样做的原因是为了创建一个用户,而不是用户



使用Doctrine,当涉及到集合或关系时,您可以告诉别名应该是什么:



http://www.doctrine-project.org/projects/orm/1.2/docs/manual/working- with-models / en



你会看到一个用户可以有很多 Phonenumber foreignAlias 是在YAML模式中设置的,因此通过 $ user-> Phonenumbers 有效访问 b

在您的示例中,您将将 foreignAlias 设置为类别,同时保留名为类别的表和记录。


Is there a convention for naming tables when using Doctrine ORM? I like to name tables with the plural but if there's a convention I want to stick to it.

So the table 'users' would be related to tables using the fk as the singular ('user_id').

Is there a best practice for this (using singular or plural table names) and if the latter, how does this apply to tables where the plural isn't a simple case of adding an 's'.

For example I currently have a table called 'categorys' instead of 'categories' to maintain the convention of adding 's'.

Is this a sensible approach?

解决方案

I used to use plural table names when using my own basic ORM but I switched over to singular table names when I started using symfony + Propel and now a little bit of Doctrine. The reason for this is for the class names because you want to create a User and not a Users.

With Doctrine, when it comes to collections or relations, you tell it what the alias should be:

http://www.doctrine-project.org/projects/orm/1.2/docs/manual/working-with-models/en

You'll see the a User can have many Phonenumber so a foreignAlias was setup in the YAML schema so it would be Phonenumbers effectively accessed via $user->Phonenumbers.

In your example you'll set the foreignAlias to be Categories while keeping the table and record named Category.

这篇关于与Doctrine ORM的表命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆