cakephp数据库表定义了三个依赖关系

时间:2013-07-31 03:42:24

标签: database cakephp multiple-tables

我有三个表(块,角色,用户),其中模式中的以下内容为真:

blocks have many users
users have many blocks
users have many roles, but the roles are specific to the block (and the user)
blocks have many roles, but the roles are specific to the user (and the block)

例如:

user => john
belongs to block => 'metro' with role => 'builder'
belongs to block => 'rural' with role => 'investor'

user => dan
belongs to block => 'metro' with role => 'resident'
belongs to block => 'rural' with role => 'investor'

我有一个名为block_role_user的连接表,其中包含以下列:

id   block_id   role_id   user_id   created   modified

我想这将是所有三张桌子之间的一种非常友好的关系。我想过用块和用户之间的hasandbelongstomany表以及角色和用户之间的另一个hasandbelongstomany表来解决这个问题,但这不会完全定义块,角色和用户之间的唯一关系。

我正在寻找有关解决此问题的最佳方法的建议,并感谢任何建议。

我研究过绑定模型,但这似乎并不适用于我在这里独特的三部分关系。

我考虑创建一个名为blockroleuser的模型,并使用标准的cakephp约定设置关系。如果这是一种可接受的方式,您能否就命名模型和控制器文件提供一些指导?

再次感谢...

1 个答案:

答案 0 :(得分:0)

如果我找到了你,那么你正在寻找一个比HABTM更松散的关系,这将定义UserBlock和{{1}之间的特定关联}}

就个人而言,我可能会使用一个使用Role定义这些关系的模型,其中belongs to可以包含许多这些关系,例如:

User

当你谈论User hasMany Whatever Whatever belongsTo User belongsTo Block belongsTo Role 模型时,这可能或多或少是你的想法,正确的命名可能取决于块究竟是什么。当然,例如BlockRoleUser甚至Involvement这样的通用名称也可能没问题,我认为你不应该为此烦恼太多。

无论如何,这看起来非常简单和严格,如果您的应用程序有必要,那么也可以使用同一个表设置其他UserBlockRole关系。这些HABTM关系最有可能仅用于选择,例如,当需要查询与HABTM Blocks相关的所有User时,或者在测试是否{{1}时}与特定的User等相关...

相关问题