CakePHP 多个 HABTM 关系

发布于 2024-10-21 06:26:10 字数 837 浏览 1 评论 0原文

我有一个资产表,其中包含所有资产类型共享的所有字段,例如 namepathfiletypesizearchitects

资产和属于 constructors 的资产。因此,我在这两种类型上设置了HABTM关系,即

      |-------- architects_assets ------- architects
assets|
      |-------- constructors_assets ----- constructors

问题是,在创建资产时,我希望每个资产都是其中之一architects_asset< /code> constructors_asset - 资产永远不会同时是架构师和构造函数资产。

简单的解决方案是创建 architect_assetsconstructors_assets 表并删除 assets 表,因为这样您就可以添加、编辑、分别查看、删除每种类型。或者,我想我可以在 assets 控制器中创建 architects_asset_addconstructors_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的笑 2024-10-28 06:26:10

我已经能够使用 hasMany through 关系来解决这个问题。这意味着为 architects_assets 和 construtors_assets 创建单独的控制器,但这适合我,因为我需要明确区分这两种资产类型。

得到的模型:

//asset
class Asset extends AppModel {
  public $hasMany = array('ArchitectAsset', 'ConstructorAsset')
}

//architect asset
class ArchitectAsset extends AppModel {
  public $belongsTo = array('Architect, Asset');
}

//architect
class Architect extends AppModel {
  public $hasMany = 'ArchitectAsset';
}

//and the same for the ConstructorAsset and Constructor models

I have been able to solve this problem using hasMany through relationships. This meant creating separate controllers for architects_assets and construtors_assets, but this suits me given I needed to make clear distinction between the 2 asset types.

The resultant models:

//asset
class Asset extends AppModel {
  public $hasMany = array('ArchitectAsset', 'ConstructorAsset')
}

//architect asset
class ArchitectAsset extends AppModel {
  public $belongsTo = array('Architect, Asset');
}

//architect
class Architect extends AppModel {
  public $hasMany = 'ArchitectAsset';
}

//and the same for the ConstructorAsset and Constructor models
违心° 2024-10-28 06:26:10

我认为修改资产模型以添加新的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文