CakePHP 多个 HABTM 关系
我有一个资产表,其中包含所有资产类型共享的所有字段,例如 name
、path
、filetype
、sizearchitects
的
资产和属于 constructors
的资产。因此,我在这两种类型上设置了HABTM关系,即
|-------- architects_assets ------- architects
assets|
|-------- constructors_assets ----- constructors
问题是,在创建资产时,我希望每个资产都是其中之一architects_asset< /code> 或
constructors_asset
- 资产永远不会同时是架构师和构造函数资产。
简单的解决方案是创建 architect_assets
和 constructors_assets
表并删除 assets
表,因为这样您就可以添加、编辑、分别查看、删除每种类型。或者,我想我可以在 assets
控制器中创建 architects_asset_add
和 constructors_asset_add
操作,但随后我必须为编辑、查看、删除,显得臃肿。
您将如何解决这个问题?
I have an assets table which contains all the fields shared by all asset types, e.g. name
, path
, filetype
, size
, etc.
Now, I have 2 asset types: assets belonging to architects
and assets belonging to construtors
. So I set up HABTM relationships on these 2 types, i.e.
|-------- architects_assets ------- architects
assets|
|-------- constructors_assets ----- constructors
The problem is, when creating an asset, I want each asset to be one of either an architects_asset
or a constructors_asset
-- assets are never both an architect and constructor asset.
The simple solution would be to create architect_assets
and constructors_assets
tables and drop the assets
table, since this way you could add, edit, view, delete each type separately. Alternatively, I'm thinking I could create architects_asset_add
and constructors_asset_add
actions in the assets
controller, but then I'd have to do the same for the edit, view and delete, which seems bloated.
How would you approach this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经能够使用 hasMany through 关系来解决这个问题。这意味着为 architects_assets 和 construtors_assets 创建单独的控制器,但这适合我,因为我需要明确区分这两种资产类型。
得到的模型:
I have been able to solve this problem using hasMany through relationships. This meant creating separate controllers for
architects_assets
andconstrutors_assets
, but this suits me given I needed to make clear distinction between the 2 asset types.The resultant models:
我认为修改资产模型以添加新的 CRUD 方法来支持这两个操作将是一个好主意。
当然,它会使您的模型代码更大,但它允许您将所有资产保留在同一个表上,以防您需要对它们执行一些与架构或构造函数关系无关的操作。
I think modifying the assets Model to add new CRUD methods to support these two actions would be a good idea.
Sure it would make you're Model code larger, but it would allow you to keep all the assets on the same table, in case you need to do some operations on them that are not related to the architecture or constructor relationship.