Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3ceff83

Browse files
committed
Prefer getExtraTableName() over static name builders in ExtraPropertyWriter
- writeAll() captures each scope bucket's table name from the matched definition; writeCommon/writeLang/writeShop now receive the extra table name instead of rebuilding it via buildExtraTableName() - Static builders stay public for the two call sites where no definition instance can exist (deleteAll sweeping all scope tables, repository column-metadata enrichment on raw rows); their docblocks now say to prefer the getters and name those remaining legitimate uses
1 parent 848ede8 commit 3ceff83

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

src/Core/ExtraProperty/Definition/ExtraPropertyDefinition.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,11 @@ public function getBaseTableName(): string
518518
/**
519519
* Returns the extra value table name for a given entity and scope.
520520
*
521-
* Use this static version only when no ExtraPropertyDefinition instance is available
522-
* (e.g. in Writer, Reader, SchemaManager which receive entity+scope as separate params).
521+
* Prefer getExtraTableName() whenever a definition instance is available. Use this
522+
* static version only when none exists: ExtraPropertyWriter::deleteAll() (sweeps all
523+
* scope tables regardless of registered definitions) and
524+
* ExtraPropertyDefinitionRepository::enrichRowsWithColumnMetadata() (runs on raw rows
525+
* before definitions can be constructed).
523526
*
524527
* @return string e.g. 'product_extra', 'product_extra_lang', 'product_extra_shop'
525528
*/
@@ -535,8 +538,10 @@ public static function buildExtraTableName(string $entityName, ExtraPropertyScop
535538
/**
536539
* Returns the storage column name for a given module and property name.
537540
*
538-
* Use this static version only when no ExtraPropertyDefinition instance is available
539-
* (e.g. in ModuleFieldsBag which holds only module name + field name as strings).
541+
* Prefer getStorageColumnName() whenever a definition instance is available. Use this
542+
* static version only when none exists:
543+
* ExtraPropertyDefinitionRepository::enrichRowsWithColumnMetadata() (runs on raw rows
544+
* before definitions can be constructed).
540545
*/
541546
public static function buildStorageColumnName(?string $moduleName, string $propertyName): string
542547
{

src/Core/ExtraProperty/Value/ExtraPropertyWriter.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public function writeAll(
6161
$entityValues = [];
6262
$langValuesByIdLang = [];
6363
$shopValues = [];
64+
$entityTableName = null;
65+
$langTableName = null;
66+
$shopTableName = null;
6467

6568
foreach ($definitions as $definition) {
6669
$moduleKey = $definition->getNormalizedModuleKey();
@@ -83,6 +86,7 @@ public function writeAll(
8386
$columnName = $definition->getStorageColumnName();
8487

8588
if (ExtraPropertyScope::LANG === $definition->getScope()) {
89+
$langTableName ??= $definition->getExtraTableName();
8690
if (is_array($value)) {
8791
// Multilang array: one entry per language.
8892
foreach ($value as $langId => $langValue) {
@@ -96,24 +100,26 @@ public function writeAll(
96100
$langValuesByIdLang[$defaultLangId][$columnName] = $value;
97101
}
98102
} elseif (ExtraPropertyScope::SHOP === $definition->getScope()) {
103+
$shopTableName ??= $definition->getExtraTableName();
99104
$shopValues[$columnName] = $value;
100105
} else {
106+
$entityTableName ??= $definition->getExtraTableName();
101107
$entityValues[$columnName] = $value;
102108
}
103109
}
104110

105111
$shopId = $shopConstraint->isSingleShopContext() ? $shopConstraint->getShopId()->getValue() : null;
106112

107-
if (!empty($entityValues)) {
108-
$this->writeCommon($entityName, $primaryKeyName, $entityId, $entityValues);
113+
if (!empty($entityValues) && null !== $entityTableName) {
114+
$this->writeCommon($entityTableName, $primaryKeyName, $entityId, $entityValues);
109115
}
110116

111-
if (!empty($langValuesByIdLang) && null !== $shopId) {
112-
$this->writeLang($entityName, $primaryKeyName, $entityId, $shopId, $langValuesByIdLang);
117+
if (!empty($langValuesByIdLang) && null !== $langTableName && null !== $shopId) {
118+
$this->writeLang($langTableName, $primaryKeyName, $entityId, $shopId, $langValuesByIdLang);
113119
}
114120

115-
if (!empty($shopValues) && null !== $shopId) {
116-
$this->writeShop($entityName, $primaryKeyName, $entityId, $shopId, $shopValues);
121+
if (!empty($shopValues) && null !== $shopTableName && null !== $shopId) {
122+
$this->writeShop($shopTableName, $primaryKeyName, $entityId, $shopId, $shopValues);
117123
}
118124
}
119125

@@ -191,23 +197,24 @@ public function deleteAll(string $entityName, string $primaryKeyName, int $entit
191197
/**
192198
* Writes common-scope (entity-level) values for one entity instance.
193199
*
200+
* @param string $extraTableName Extra table name without DB prefix (from ExtraPropertyDefinition::getExtraTableName())
194201
* @param array<string, mixed> $columnValues
195202
*/
196-
protected function writeCommon(string $entityName, string $primaryKeyName, int $entityId, array $columnValues): void
203+
protected function writeCommon(string $extraTableName, string $primaryKeyName, int $entityId, array $columnValues): void
197204
{
198-
$fullTableName = $this->prefix . ExtraPropertyDefinition::buildExtraTableName($entityName, ExtraPropertyScope::COMMON);
199-
$sql = $this->buildUpsertSql($fullTableName, $primaryKeyName, [], $columnValues);
205+
$sql = $this->buildUpsertSql($this->prefix . $extraTableName, $primaryKeyName, [], $columnValues);
200206
$this->connection->executeStatement($sql, [$entityId, ...array_values($columnValues)]);
201207
}
202208

203209
/**
204210
* Writes lang-scope values for one entity instance, one row per language.
205211
*
212+
* @param string $extraTableName Extra table name without DB prefix (from ExtraPropertyDefinition::getExtraTableName())
206213
* @param array<int, array<string, mixed>> $langValuesByIdLang [idLang => ['column' => value]]
207214
*/
208-
protected function writeLang(string $entityName, string $primaryKeyName, int $entityId, int $shopId, array $langValuesByIdLang): void
215+
protected function writeLang(string $extraTableName, string $primaryKeyName, int $entityId, int $shopId, array $langValuesByIdLang): void
209216
{
210-
$fullTableName = $this->prefix . ExtraPropertyDefinition::buildExtraTableName($entityName, ExtraPropertyScope::LANG);
217+
$fullTableName = $this->prefix . $extraTableName;
211218

212219
foreach ($langValuesByIdLang as $idLang => $columnValues) {
213220
if (empty($columnValues)) {
@@ -221,12 +228,12 @@ protected function writeLang(string $entityName, string $primaryKeyName, int $en
221228
/**
222229
* Writes shop-scope values for one entity instance.
223230
*
231+
* @param string $extraTableName Extra table name without DB prefix (from ExtraPropertyDefinition::getExtraTableName())
224232
* @param array<string, mixed> $columnValues
225233
*/
226-
protected function writeShop(string $entityName, string $primaryKeyName, int $entityId, int $shopId, array $columnValues): void
234+
protected function writeShop(string $extraTableName, string $primaryKeyName, int $entityId, int $shopId, array $columnValues): void
227235
{
228-
$fullTableName = $this->prefix . ExtraPropertyDefinition::buildExtraTableName($entityName, ExtraPropertyScope::SHOP);
229-
$sql = $this->buildUpsertSql($fullTableName, $primaryKeyName, ['id_shop'], $columnValues);
236+
$sql = $this->buildUpsertSql($this->prefix . $extraTableName, $primaryKeyName, ['id_shop'], $columnValues);
230237
$this->connection->executeStatement($sql, [$entityId, $shopId, ...array_values($columnValues)]);
231238
}
232239

0 commit comments

Comments
 (0)