CakePHP:间接关联模型的缺点

发布于 2024-08-31 23:23:45 字数 611 浏览 14 评论 0原文

我在处理 cakephp 中的间接关联模型时遇到一些问题。我当前的模型设置如下:

Deliveries hasOne License
License belongsTo Delivery
License hasAndBelongsToMany Product (and vice-versa)
License hasAndBelongsToMany ProductOption (and vice-versa)

我试图将所有这些模型的信息保存在一个表单中。我遇到的缺点如下:

  1. 表单助手似乎只能看到一层深的字段类型。

  2. saveAll() 似乎只能保存一层深度的记录(对于多个模型表单)。

我正在到处寻找这些问题的解决方案,但由于我是 CakePHP 的新手,我不确定处理这些问题的最新方法或“正确”方法是什么。

非常感谢任何建议。谢谢大家!

编辑:我已将代码发布到此处失败的尝试: http://bin.cakephp.org/saved /58501

I'm having some issues with dealing with indirectly associated models in cakephp. My current model setup is as follows:

Deliveries hasOne License
License belongsTo Delivery
License hasAndBelongsToMany Product (and vice-versa)
License hasAndBelongsToMany ProductOption (and vice-versa)

I'm trying to save information about ALL of these models inside ONE form. The shortcomings I'm running into are the following:

  1. The form helper only seems able to see the field type one level deep.

  2. saveAll() only seems able to save records one level deep (for multiple model forms).

I'm searching everywhere for the solutions to these issues, but since I'm new to CakePHP, I'm not sure what the newest methods or "correct" methods are for dealing with these issues.

Any advice is much appreciated. Thank you all!

EDIT: I've posted code to my failed attempt here: http://bin.cakephp.org/saved/58501

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

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

发布评论

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

评论(2

っ左 2024-09-07 23:23:45

saveAll() 似乎只能保存一层深度的记录(对于多个模型表单)。

我过去偶然发现了这个限制,当时选择通过将我的表单分解为多个较小的表单来解决它。

使用 saveAll 和 InnoDB 表时要记住的一件事是,您将获得原子事务,因为如果 Cake 无法向数据库提交更改,它将执行回滚。

因此,虽然您显然可以使用自己的几行代码来解决该问题(因为 Cake 的 saveAll 单行代码没有达到预期),但如果需要进行事务处理,您将不得不花费更多时间。

saveAll() only seems able to save records one level deep (for multiple model forms).

I have stumbled across this limitation in the past and chose, at the time, to work around it by breaking up my form into multiple smaller forms.

One thing to bear in mind when using saveAll and InnoDB tables is that you get atomic transactions, as Cake will perform a rollback if it is unable to commit changes to the database.

So, while you can obviously work around the issue with a few lines of your own code (since Cake's saveAll one-liner doesn't live up to expectations), you would have to spend more time if transactions were a requirement.

自此以后,行同陌路 2024-09-07 23:23:45

您好,我知道这是一篇旧帖子,但我想发布此帖子是为了帮助其他人,从 CakePHP 2.1 开始,您可以使用调用 saveAll() 时的选项来保存多个级别的模型关联,更多详细信息请参见: http ://book.cakephp.org/2.0/en/appendices/new-features-in-cakephp-2-1.html#model-saveall-model-savelinked-model-validatelinked

示例:

$this->MyModel->saveAll($this->request->data, array('deep'=>true));

对于 FormHelper限制,我和你一样迷失,我可能会手动设置输入类型,因为我的字段不需要太多验证。

Hi I know this is an old post but thought I'd post this to help others, As of CakePHP 2.1 you can save multiple levels of model association using an option on the call the saveAll(), more details here: http://book.cakephp.org/2.0/en/appendices/new-features-in-cakephp-2-1.html#model-saveall-model-saveassociated-model-validateassociated

Example:

$this->MyModel->saveAll($this->request->data, array('deep'=>true));

As for the FormHelper limitation, I'm as lost as you are, I will probably resort to setting the input types manually as my fields don't need too much validation.

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