I don’t know if it’s a bug or if it’s a feature.
I have a transaction where I want to save an entity and get its ID for another entity I want to save. But the entity doesn’t contain the ID after the save is successful.
$this->loadModel('Farms');
$this->loadModel('ContractProductions');
$dbo = ConnectionManager::get($this->Farms->defaultConnectionName());
$dbo->begin();
foreach ($sheetDataForImport as $row => $farm) {
$farmEntity = $this->Farms->newEntity($farm);
$this->Farms->save($farmEntity);
$saveError = $farmEntity->errors();
if (!empty($saveError)) {
$importErrors[$row] = $saveError;
}
elseif(array_key_exists($row,$contractProductionsForImport)) {
**$contractProductionsForImport[$row]['farm_id'] = $farmEntity->id;**
$contractProductionEntity = $this->ContractProductions->newEntity($contractProductionsForImport[$row]);
$this->ContractProductions->save($contractProductionEntity);
$saveError = $contractProductionEntity->errors();
if (!empty($saveError)) {
$importErrors[$row] = $saveError;
}
}
}
if (empty($importErrors)) {
//$dbo->commit();
$dbo->rollback();
} else {
$dbo->rollback();
//Log::debug($importErrors);
}
It was a Model limitation in beforeFind, which was the problem…