Skip to content

Commit

Permalink
FRW-7452 Fixed type casting for child relations. (#10863)
Browse files Browse the repository at this point in the history
FRW-7452 Fixed type casting for child relations.
  • Loading branch information
vol4onok authored Mar 11, 2024
1 parent d2a19e9 commit ee8e512
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Generated\Shared\Transfer\DynamicEntityConfigurationRelationTransfer;
use Generated\Shared\Transfer\DynamicEntityConfigurationTransfer;
use Generated\Shared\Transfer\DynamicEntityCriteriaTransfer;
use Generated\Shared\Transfer\DynamicEntityDefinitionTransfer;
use Generated\Shared\Transfer\DynamicEntityFieldConditionTransfer;
use Generated\Shared\Transfer\DynamicEntityFieldDefinitionTransfer;
use Generated\Shared\Transfer\DynamicEntityTransfer;
Expand Down Expand Up @@ -456,8 +455,6 @@ protected function processActiveRecordSaving(
ActiveRecordInterface $activeRecord,
bool $isChildEntity = false
): DynamicEntityCollectionResponseTransfer {
$dynamicEntityDefinitionTransfer = $dynamicEntityConfigurationTransfer->getDynamicEntityDefinitionOrFail();

/** @var \Propel\Runtime\ActiveRecord\ActiveRecordInterface $activeRecord */
$activeRecord = $this->getFactory()->createDynamicEntityMapper()->mapDynamicEntityTransferToDynamicEntity(
$dynamicEntityTransfer,
Expand All @@ -477,8 +474,11 @@ protected function processActiveRecordSaving(
throw $exception;
}

$dynamicEntityTransfer = $this->buildDynamicEntityFields($dynamicEntityTransfer, $dynamicEntityDefinitionTransfer, $activeRecord);
$dynamicEntityTransfer = $this->addIdentifierToFields($dynamicEntityTransfer, $dynamicEntityDefinitionTransfer, $activeRecord, $dynamicEntityConfigurationTransfer);
$dynamicEntityTransfer = $this->getFactory()->createDynamicEntityMapper()->mapEntityRecordToDynamicEntityTransfer(
$activeRecord,
$dynamicEntityConfigurationTransfer->getDynamicEntityDefinitionOrFail(),
$dynamicEntityTransfer,
);

if ($isChildEntity === false) {
$dynamicEntityCollectionResponseTransfer->addDynamicEntity($dynamicEntityTransfer);
Expand Down Expand Up @@ -591,63 +591,6 @@ protected function addErrorToResponseTransfer(
return $dynamicEntityCollectionResponseTransfer->addError($errorMessageTransfer);
}

/**
* @param \Generated\Shared\Transfer\DynamicEntityTransfer $dynamicEntityTransfer
* @param \Generated\Shared\Transfer\DynamicEntityDefinitionTransfer $dynamicEntityDefinitionTransfer
* @param \Propel\Runtime\ActiveRecord\ActiveRecordInterface $activeRecord
* @param \Generated\Shared\Transfer\DynamicEntityConfigurationTransfer $dynamicEntityConfigurationTransfer
*
* @return \Generated\Shared\Transfer\DynamicEntityTransfer
*/
protected function addIdentifierToFields(
DynamicEntityTransfer $dynamicEntityTransfer,
DynamicEntityDefinitionTransfer $dynamicEntityDefinitionTransfer,
ActiveRecordInterface $activeRecord,
DynamicEntityConfigurationTransfer $dynamicEntityConfigurationTransfer
): DynamicEntityTransfer {
$identifier = $dynamicEntityDefinitionTransfer->getIdentifierOrFail();

$identifierVisibleName = $this->getIdentifierVisibleName($identifier, $dynamicEntityConfigurationTransfer);

$identifierValue = $activeRecord->getByName($identifier);
$dynamicEntityTransfer->setFields(array_merge(
$dynamicEntityTransfer->getFields(),
[$identifierVisibleName => $identifierValue],
));
$dynamicEntityTransfer->setIdentifier($identifierValue);

return $dynamicEntityTransfer;
}

/**
* @param \Generated\Shared\Transfer\DynamicEntityTransfer $dynamicEntityTransfer
* @param \Generated\Shared\Transfer\DynamicEntityDefinitionTransfer $dynamicEntityDefinitionTransfer
* @param \Propel\Runtime\ActiveRecord\ActiveRecordInterface $activeRecord
*
* @return \Generated\Shared\Transfer\DynamicEntityTransfer
*/
protected function buildDynamicEntityFields(
DynamicEntityTransfer $dynamicEntityTransfer,
DynamicEntityDefinitionTransfer $dynamicEntityDefinitionTransfer,
ActiveRecordInterface $activeRecord
): DynamicEntityTransfer {
$activeRecord = $activeRecord->toArray();
$entityFields = [];
foreach ($dynamicEntityDefinitionTransfer->getFieldDefinitions() as $fieldDefinitionTransfer) {
$fieldName = $fieldDefinitionTransfer->getFieldNameOrFail();
$fieldVisibleName = $fieldDefinitionTransfer->getFieldVisibleNameOrFail();
$fieldValue = $activeRecord[$fieldName];
$entityFields[$fieldVisibleName] = $fieldValue;
}

$dynamicEntityTransfer->setFields(array_merge(
$dynamicEntityTransfer->getFields(),
$entityFields,
));

return $dynamicEntityTransfer;
}

/**
* @param string $identifier
* @param \Generated\Shared\Transfer\DynamicEntityConfigurationTransfer $dynamicEntityConfigurationTransfer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ public function mapEntityRecordsToCollectionTransfer(
return $dynamicEntityCollectionTransfer;
}

/**
* @param \Propel\Runtime\ActiveRecord\ActiveRecordInterface $entityRecord
* @param \Generated\Shared\Transfer\DynamicEntityDefinitionTransfer $dynamicEntityDefinitionTransfer
* @param \Generated\Shared\Transfer\DynamicEntityTransfer $dynamicEntityTransfer
*
* @return \Generated\Shared\Transfer\DynamicEntityTransfer
*/
public function mapEntityRecordToDynamicEntityTransfer(
ActiveRecordInterface $entityRecord,
DynamicEntityDefinitionTransfer $dynamicEntityDefinitionTransfer,
DynamicEntityTransfer $dynamicEntityTransfer
): DynamicEntityTransfer {
$indexedFieldDefinitions = $this->indexDynamicEntityFieldDefinitionsByTableFieldName($dynamicEntityDefinitionTransfer);
$identifierVisibleName = $this->getIdentifierVisibleName($dynamicEntityDefinitionTransfer->getIdentifierOrFail(), $dynamicEntityDefinitionTransfer);
$dynamicEntityFields = $this->mapRecordFieldsToDynamicEntityFieldsArray($entityRecord, $indexedFieldDefinitions);
$dynamicEntityTransfer
->setFields($dynamicEntityFields)
->setIdentifier($dynamicEntityFields[$identifierVisibleName]);

return $dynamicEntityTransfer;
}

/**
* @param array<mixed> $dynamicEntityConfigurationData
* @param \Generated\Shared\Transfer\DynamicEntityConfigurationCollectionTransfer $dynamicEntityConfigurationCollectionTransfer
Expand Down

0 comments on commit ee8e512

Please sign in to comment.