From f5dd1e399e6b918bae552ccb581240dbbabedcc2 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 5 Feb 2024 17:38:48 -0500 Subject: [PATCH] refactor: Finish updating content data --- src/services/Assets.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/services/Assets.php b/src/services/Assets.php index 7db0531..60bfd6b 100644 --- a/src/services/Assets.php +++ b/src/services/Assets.php @@ -198,10 +198,26 @@ protected function updateContent(string $value, CantoFieldData $cantoFieldData, $jsonSearchSql = $this->pgSqlJsonContains($cantoAssetDataFieldName, $jsonSearchNeedle); } $rows = (new Query()) - ->select([$cantoAssetDataFieldName]) + ->select(['id', $cantoAssetDataFieldName]) ->from([$table]) - ->where([$cantoIdFieldName => 0, $jsonSearchSql]) + ->where([$cantoIdFieldName => 0]) + ->addParams([$jsonSearchSql]) ->all(); + // Iterate through each row, the field data as appropriate + foreach ($rows as $row) { + $rowCollection = new Collection($row[$cantoAssetDataFieldName]); + $rowCollection->transform(function ($item) use ($cantoFieldData) { + if ($item['id'] === $cantoFieldData->cantoId) { + $item = $cantoFieldData->cantoAssetData[0]; + } + return $item; + }); + try { + $rowsAffected = Db::update($table, [$cantoAssetDataFieldName => $rowCollection->all()], ['id' => $row['id']]); + } catch (Exception $e) { + Craft::error($e->getMessage(), __METHOD__); + } + } } } }