Skip to content

Commit

Permalink
refactor: Fold columns back into update()
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Feb 5, 2024
1 parent ca5e903 commit f0d3c2a
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/services/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ protected function updateContent(string $value, CantoFieldData $cantoFieldData,
// Find any $queryColumn content column row that match $value, and update them with the data from $cantoFieldData
$queryColumn = ElementHelper::fieldColumnFromField($cantoDamAssetField, $contentColumnKey);
if ($queryColumn) {
$columns = $this->getColumns($cantoDamAssetField, $cantoFieldData);
$columns = [];
foreach (self::CONTENT_COLUMN_KEY_MAPPINGS as $propertyName => $selectColumnKey) {
$columns[ElementHelper::fieldColumnFromField($cantoDamAssetField, $selectColumnKey)] = $cantoFieldData->$propertyName;
}
try {
$rows = Db::update($table, $columns, [$queryColumn => $value]);
} catch (Exception $e) {
Expand All @@ -197,7 +200,7 @@ protected function updateContent(string $value, CantoFieldData $cantoFieldData,
$rows = (new Query())
->select([$cantoAssetDataFieldName])
->from([$table])
->where([$cantoIdFieldName => 0, $cantoAssetDataFieldName => $jsonSearchSql])
->where([$cantoIdFieldName => 0, $jsonSearchSql])
->all();
}
}
Expand Down Expand Up @@ -240,21 +243,4 @@ private function getBlockFields(string $fieldType): array
/** @phpstan-ignore-next-line */
return Craft::$app->getFields()->getFieldsByType($fieldType);
}

/**
* Return the $cantoDamAssetField db columns to update with the values from the $cantoFieldData
*
* @param FieldInterface $cantoDamAssetField
* @param CantoFieldData $cantoFieldData
* @return array
*/
private function getColumns(FieldInterface $cantoDamAssetField, CantoFieldData $cantoFieldData): array
{
$columns = [];
foreach (self::CONTENT_COLUMN_KEY_MAPPINGS as $propertyName => $selectColumnKey) {
$columns[ElementHelper::fieldColumnFromField($cantoDamAssetField, $selectColumnKey)] = $cantoFieldData->$propertyName;
}

return $columns;
}
}

0 comments on commit f0d3c2a

Please sign in to comment.