CakePhp saveAssociated deep = true不起作用

amodi 发布于 2019-03-09 cakephp 最后更新 2019-03-09 14:38 3 浏览

我正在尝试使用saveAssociated和深层选项设置为true来保存数组,如下所示,但它不保存答案,只有测验和问题。我使用CakePhp 2.3.1

$myarray = array(
    'Quiz' => array(
        'title' => 'Test di Prova copy',
        'description' => '',
        'timer' => '5',
        'days' => '10',
        'tries' => '2',
        'pass_percentage' => '90',
        'active' => true
    ),
    'Question' => array(
        (int) 0 => array(
            'question' => 'Quanti valori può assumere un bit?',
            'score' => '10',
            'position' => '0',
            'created' => '0000-00-00 00:00:00',
            'modified' => '2013-04-10 13:04:50',
            'Answer' => array(
                (int) 0 => array(
                    'answer' => '2',
                    'right' => true
                ),
                (int) 1 => array(
                    'answer' => '4',
                    'right' => false
                ),
                (int) 2 => array(
                    'answer' => '8',
                    'right' => false
                ),
                (int) 3 => array(
                    'answer' => 'non lo sò
',
                    'right' => false
                )
            )
        ),
        (int) 1 => array(
            'question' => 'Quale tra questi tag Html identifica l'header più grande di dimensioni?',
            'score' => '10',
            'position' => '2',
            'created' => '0000-00-00 00:00:00',
            'modified' => '2013-04-10 13:04:50',
            'Answer' => array(
                (int) 0 => array(
                    'answer' => 'H6',
                    'right' => false
                ),
                (int) 1 => array(
                    'answer' => 'H4',
                    'right' => false
                ),
                (int) 2 => array(
                    'answer' => 'H1',
                    'right' => true
                )
            )
        ),
        (int) 2 => array(
            'question' => 'Da quanti bit è composto un byte?',
            'score' => '10',
            'position' => '1',
            'created' => '0000-00-00 00:00:00',
            'modified' => '2013-04-10 13:04:50',
            'Answer' => array(
                (int) 0 => array(
                    'answer' => '2',
                    'right' => false
                ),
                (int) 1 => array(
                    'answer' => '4',
                    'right' => false
                ),
                (int) 2 => array(
                    'answer' => '6',
                    'right' => false
                ),
                (int) 3 => array(
                    'answer' => '8',
                    'right' => true
                )
            )
        )
    )
);
$ this-> Quiz-> saveAssociated($ myarray,array('deep'=> true));
已邀请:

non_at

赞同来自:

那个数据结构可以和saveAll一起使用,你试过吗?

<?php
$this->Model1->saveAll($myarray, array('deep' => true));
?>
saveAll在内部调用saveAssociated。 我相信这应该适合你。