2017-04-26 51 views
0

我有這樣的關聯,如company ==> hasMany CohortsCohorts ==> contains hasMany與其他6個表,例如:CohortTimeConfigcakephp3中的深層關聯不保存數據

現在我從組羣表中檢索默認值,這就好了。

$cohorts = $this->Companies->Cohorts 
     ->find('all', [ 
      'conditions' => [ 
       'company_id IS NULL and status_id = 1' 
      ], 
      'contain' => [ 
       'CohortConfigs', 
       'CohortPpmCategories', 
       'CohortRanges', 
       'CohortRoadClassConfig', 
       'CohortTimeConfig', 
       'CohortWeatherConfig' 
      ], 
      'order' => ['id' => 'ASC'] 
     ]) 
     ->toArray(); 

現在我在做什麼正在改變着$同夥這樣

$company_id = $company->id; 
array_walk($cohorts, function (&$cohort, $index) use ($company_id) { 
    unset($cohort['id']); 
    $cohort['company_id'] = $company_id; 

    //cohort_time_config 
    if(!empty($cohort['cohort_time_config'])){ 
     array_walk($cohort['cohort_time_config'], function (&$cohort_time_config, $key) { 
      unset($cohort_time_config['id']); 
      unset($cohort_time_config['cohort_id']); 
     }); 
    } 
}); 

在結束我試圖與關聯保存所有值曾這樣

$allCohorts = $this->Companies->Cohorts->newEntity(); 
$allCohorts = $this->Companies->Cohorts->patchEntity($allCohorts, $cohorts); 

debug(
    $this->Companies->Cohorts->save($allCohorts,[ 
     'atomic'=>true, 
     'validate' => true, 
     'associated' => [ 
      'CohortConfigs', 
      'CohortPpmCategories', 
      'CohortRanges', 
      'CohortRoadClassConfig', 
      'CohortTimeConfig', 
      'CohortWeatherConfig' 
     ] 
    ]) 
); 

我想提到我的//debug($allCohorts->errors());是沒有錯誤。這意味着所有的規則檢查都可以。但數據沒有得到保存。

任何人都可以幫助我嗎?以及如何調試它爲什麼不保存數據?

+1

在發佈代碼時,請花時間以可讀的方式進行格式化,並確保其語法正確(您的第二個代碼塊缺少關閉的圓括號和分號) - 謝謝! – ndm

+0

對不起。編輯。謝謝 – user2480902

回答

0

您的查詢看起來像許多數據檢索和想節省很多數據,但你使用像saveing一個數據
你的實體

$allCohorts = $this->Companies->Cohorts->newEntity(); 
$allCohorts = $this->Companies->Cohorts->patchEntity($allCohorts, $cohorts); 

應該像

$allCohorts = $this->Companies->Cohorts->newEntities(); 
$allCohorts = $this->Companies->Cohorts->patchEntities($allCohorts, $cohorts); 

;您也可以使用

$allCohorts = $this->Companies->Cohorts->newEntities($allCohorts); 
$this->Companies->Cohorts->saveMany($allCohorts); 

欲瞭解更多信息,請參閱Saving Multiple Entities

+0

Hi @ tarikul05我嘗試過newEntities()和patchEntities()以及saveMany()。但沒有運氣 – user2480902

+0

您嘗試過的「相關」數組包括?它應該包括 – tarikul05

+0

是啊,我已經包含'關聯'數組。 :( – user2480902