CakePHP hasAndBelogsToMany 使用 save() 与 saveAll()
我正在使用一个非常内在的数据库和 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->id
和 save() 中
保存了 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 INSERT
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您下载nightly,则此问题已修复。
但要小心,其他东西可能会损坏。
This is fixed if you download the nightly.
Be careful though, something else might break.
saveAll() 和 HABTM 关联的问题是一个已知的 CakePHP 问题,并且尚未解决自 1.2 RC2 起。
作为保存相关模型数据的最佳实践,根据 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:
However, using saveAll() and save() should work, and IMHO is a more flexible/generic solution.