首页 文章

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

提问于
浏览
2

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

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

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

我正在寻找有关最佳方法的建议,并感谢任何建议 .

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

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

再次感谢...

1 回答

  • 0

    如果我找到了你,那么你正在寻找一个比 HABTM 更松散的关系,这将定义 UserBlockRole 之间的特定连接 .

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

    User hasMany Whatever
    
    Whatever belongsTo User
             belongsTo Block
             belongsTo Role
    

    当你谈论一个 BlockRoleUser 模型时,这可能或多或少都与你有关,正确的命名可能取决于块究竟是什么 . 当然,例如 Involvement 甚至 UserBlockRole 这样的通用名称也可能没问题,我认为你不应该为此烦恼太多 .

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

相关问题