2013-04-11 73 views
0

我想保存一個數組,如下所示與saveAssociated和深層選項設置爲true,但它不是保存答案,只有測驗和問題。我使用CakePHP 2.3.1CakePhp saveAssociated深=真不工作

$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 
       ) 
      ) 
     ) 
    ) 
); 

$這 - > Quiz-> saveAssociated($ myarray中,陣列( '深'=>真));

+0

1)請嘗試使用真實的模型名稱,2)包含您的關聯,3)您爲什麼要保存空的數據? – Dave 2013-04-11 16:44:58

+0

根據你的代碼,我正確地認爲'Model1' hasMany' Model2' hasMany'Model3'? – 2013-04-11 20:04:08

+0

我使用真實的代碼更新了上面的代碼,以避免誤解。 – user2271228 2013-04-12 07:24:08

回答

1

該數據結構可以與saveAll一起使用,你試過了嗎?

<?php 

$this->Model1->saveAll($myarray, array('deep' => true)); 

?> 

saveAll內部調用saveAssociated。

我相信應該適合你。

+0

這解決了我的類似問題。 – ADDruid 2014-05-23 00:22:47