CakePHP 保存三模型关系关联 [英] CakePHP save a three-model relationship association

查看:32
本文介绍了CakePHP 保存三模型关系关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下输出需要插入到数据库中:

I have the following output which I need to be inserted in the database:

Array
(
[Test] => Array
    (
    )

[Question] => Array
    (
        [0] => Array
            (
                [category_id] => 3
                [answer_style_id] => 2
                [Answer] => Array
                    (
                        [0] => Array
                            (
                                [capital_category_id] => 14
                                [correct] => 1
                            )

                       ...
         ...

简单来说,每个测试都有很多问题,每个问题都有很多答案,每个关联的模型都有一个需要由 Cake 设置的外键(每个问题都有一个 test_id,每个答案都有一个 question_id).

Briefly, each Test hasMany Questions, and each Question hasMany Answer, with each associated model having a foreign key which need to be set by Cake (each Question has a test_id, and each Answer has a question_id).

问题是当我$this->Test->saveAll($data);时,只有测试和问题被保存,而不是答案.

The problem is that when I $this->Test->saveAll($data);, only the Test and the Questions get saved, not the answers.

如何保存所有数据,让 Cake 自动为每个关联模型设置外键?

How can I save all data, with Cake automagically setting the foreign key for each associated model?

谢谢!

推荐答案

是的,从 CakePHP 2.1 开始你可以这样保存深度模型树

Yes, you can save deep model trees since CakePHP 2.1 this way

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

参考这里> http://book.cakephp.org/2.0/en/appendices/new-features-in-cakephp-2-1.html#model-saveall-model-saveassociated-model-validateassociated

这篇关于CakePHP 保存三模型关系关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆