CakePHP hasAndBelogsToMany 使用 save() 与 saveAll()

发布于 2024-07-06 20:58:04 字数 1264 浏览 4 评论 0原文

我正在使用一个非常内在的数据库和 CakePHP 应用程序,到目前为止我的多模型视图和控制器工作正常。 我有一个单一的表(Entity),它在其他几个表上的id作为外键entity_id

有些表是一对一的关系(就像公司是一个实体),有些是一对多(实体可以有多个地址)等等在。

我不会/不能更改数据库模型,所以这就是结构。

我一直在使用 saveAll() 来保存那些输入名称如下的表上的数据:

Entity.type='x' (hidden inside the view)
Company.name
Address.0.street
Address.0.city
Address.1.street
Address.1.city

... and so on ...

我的 save all 正在完成所有艰苦的工作,BEGIN TRANSACTION,所有 INSERT 和最终的 COMMIT ...

但是现在我已经创建了一个 EntityCategory ,它是一个 to n 关系,并创建了完整的 HABTM< /code> 模型内部的关系。

当我 save() 时它起作用,但只是 HABTM 关系,并且当我使用 saveAll() 时它会保存所有内容(就像以前一样) HABTM 关系除外。

我错过了什么吗? 我如何使其正确工作? 我今天使用以下代码:

if (!empty($this->data)) {
  $this->Entity->saveAll($this->data);
  $this->Entity->save($this->data);
}

saveAll() 将所有数据保存在多个表中,将 id 保存在 Entity->idsave() 中 保存了 HABTM 关系,但我不确定它是否正确,或者如果我更改某些结构/模型是否会给我带来问题。

这是最好的使用方式吗? 是否有正确的方法来保存 CakePHP 中的关系? 你的经验/知识可以告诉我什么?

I am using a very intrinsic database with a CakePHP application and so far my multi-models views and controllers are working fine. I have a singular table (Entity) that have it's id on several other tables as the Foreign Key entity_id

Some tables are one to one relations (Like a Company is one Entity) and some are one to many (Entity can have several Addresses) and so on.

I won't/can't change the database model, so this is the structure.

I have been using saveAll() to save data on those tables with input names like:

Entity.type='x' (hidden inside the view)
Company.name
Address.0.street
Address.0.city
Address.1.street
Address.1.city

... and so on ...

and my save all is doing all the hard job, BEGIN TRANSACTION, all INSERTs and a final COMMIT ...

But now I've created a EntityCategory that is a n to n relation and created the full HABTM relation inside the model.

It works when I save() it but just the HABTM relation, and it saves everthing when I use saveAll() (just as before) except for the HABTM relation.

Am I missing something ? How I make this work correctly ? I am using the following code today:

if (!empty($this->data)) {
  $this->Entity->saveAll($this->data);
  $this->Entity->save($this->data);
}

The saveAll() saves all data in several tables, saves the id in Entity->id and the save() saves the HABTM relations, but I am not sure if it is correct or if it can bring me problems if I change some structure/model.

Is this the best way to use it? Is there a correct way to save that relations inside CakePHP ? What your experience/knowledge can tell me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

椒妓 2024-07-13 20:58:04

如果您下载nightly,则此问题已修复。

但要小心,其他东西可能会损坏。

This is fixed if you download the nightly.

Be careful though, something else might break.

寄人书 2024-07-13 20:58:04

saveAll() 和 HABTM 关联的问题是一个已知的 CakePHP 问题,并且尚未解决自 1.2 RC2 起。

作为保存相关模型数据的最佳实践,根据 CakePHP 食谱

“使用关联模型时,重要的是要认识到保存模型数据应始终由相应的 CakePHP 模型完成。如果您要保存新帖子及其关联的评论,那么您将同时使用帖子和评论模型在保存操作期间。”

然而,使用 saveAll() 和 save() 应该可以工作,恕我直言,这是一个更灵活/通用的解决方案。

The problem with saveAll() and HABTM associations is a known CakePHP issue, and has not been resolved as of 1.2 RC2.

As fas as best pratices for saving related model data goes, according to the CakePHP cookbook:

"When working with associated models, it is important to realize that saving model data should always be done by the corresponding CakePHP model. If you are saving a new Post and its associated Comments, then you would use both Post and Comment models during the save operation."

However, using saveAll() and save() should work, and IMHO is a more flexible/generic solution.

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