From efc18577a34990c3835ca35090fc3116aa7aa1c3 Mon Sep 17 00:00:00 2001 From: Dominik Grothaus Date: Tue, 2 Jul 2024 14:19:31 +0200 Subject: [PATCH] =?UTF-8?q?[Translatable]=20Ignore=20notInsertable=20colum?= =?UTF-8?q?ns=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit in postPersist hook. Respect the column's metadata and don't attempt to insert a column that's flagged to be not-insertable. --- CHANGELOG.md | 2 ++ src/Translatable/Mapping/Event/Adapter/ODM.php | 10 ++++++++-- src/Translatable/Mapping/Event/Adapter/ORM.php | 11 ++++++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea079941e4..76b0695171 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ a release. --- ## [Unreleased] +### Fixed +- Translatable: Ignore notInsertable columns in postPersist hook ## [3.16.1] ### Fixed diff --git a/src/Translatable/Mapping/Event/Adapter/ODM.php b/src/Translatable/Mapping/Event/Adapter/ODM.php index be64177ce9..03e70d10c4 100644 --- a/src/Translatable/Mapping/Event/Adapter/ODM.php +++ b/src/Translatable/Mapping/Event/Adapter/ODM.php @@ -139,9 +139,15 @@ public function insertTranslationRecord($translation) $data = []; foreach ($meta->getReflectionProperties() as $fieldName => $reflProp) { - if (!$meta->isIdentifier($fieldName)) { - $data[$meta->getFieldMapping($fieldName)['name']] = $reflProp->getValue($translation); + if ($meta->isIdentifier($fieldName)) { + continue; } + $fieldMappings = $meta->getFieldMapping($fieldName); + if (($fieldMappings['notInsertable'] ?? false) === true) { + continue; + } + + $data[$meta->getColumnName($fieldName)] = $reflProp->getValue($translation); } $insertResult = $collection->insertOne($data); diff --git a/src/Translatable/Mapping/Event/Adapter/ORM.php b/src/Translatable/Mapping/Event/Adapter/ORM.php index 923f60cdc2..355e3ccce0 100644 --- a/src/Translatable/Mapping/Event/Adapter/ORM.php +++ b/src/Translatable/Mapping/Event/Adapter/ORM.php @@ -195,11 +195,16 @@ public function insertTranslationRecord($translation) $data = []; foreach ($meta->getReflectionProperties() as $fieldName => $reflProp) { - if (!$meta->isIdentifier($fieldName)) { - $data[$meta->getColumnName($fieldName)] = $reflProp->getValue($translation); + if ($meta->isIdentifier($fieldName)) { + continue; + } + $fieldMappings = $meta->getFieldMapping($fieldName); + if (($fieldMappings['notInsertable'] ?? false) === true) { + continue; } - } + $data[$meta->getColumnName($fieldName)] = $reflProp->getValue($translation); + } $table = $meta->getTableName(); if (!$em->getConnection()->insert($table, $data)) { throw new RuntimeException('Failed to insert new Translation record');