我将company ==> hasMany Cohorts
和Cohorts ==> contains hasMany
与其他6个表格CohortTimeConfig
进行关联。
现在我正在从Cohorts表中检索默认值,这很好。
$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());
没有错误。这意味着所有规则检查都可以。但数据没有得到保存。
任何人都可以帮助我吗?以及如何调试它为什么不保存数据?
答案 0 :(得分:0)
您的查询看起来像许多数据检索并希望保存许多数据,但您使用像保存一个数据一样 您的实体
$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