我有這樣的關聯,如company ==> hasMany Cohorts
和Cohorts ==> contains hasMany
與其他6個表,例如:CohortTimeConfig
。cakephp3中的深層關聯不保存數據
現在我從組羣表中檢索默認值,這就好了。
$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());
是沒有錯誤。這意味着所有的規則檢查都可以。但數據沒有得到保存。
任何人都可以幫助我嗎?以及如何調試它爲什麼不保存數據?
在發佈代碼時,請花時間以可讀的方式進行格式化,並確保其語法正確(您的第二個代碼塊缺少關閉的圓括號和分號) - 謝謝! – ndm
對不起。編輯。謝謝 – user2480902