|
35 | 35 | use Contao\System; |
36 | 36 | use Contao\Validator; |
37 | 37 | use Doctrine\DBAL\Connection; |
| 38 | +use Doctrine\DBAL\Query\QueryBuilder; |
38 | 39 | use MetaModels\Attribute\TranslatedReference; |
39 | 40 | use MetaModels\Helper\ToolboxFile; |
40 | 41 | use MetaModels\IMetaModel; |
@@ -178,6 +179,9 @@ protected function getValueTable() |
178 | 179 | * @param string|string[] $mixLangCode The language code/s to use, optional. |
179 | 180 | * |
180 | 181 | * @return array |
| 182 | + * |
| 183 | + * @deprecated This is deprecated since 2.1 and where removed in 3.0. |
| 184 | + * Implement your own replacement for this. |
181 | 185 | */ |
182 | 186 | protected function getWhere($mixIds, $mixLangCode = '') |
183 | 187 | { |
@@ -210,6 +214,46 @@ protected function getWhere($mixIds, $mixLangCode = '') |
210 | 214 | ]; |
211 | 215 | } |
212 | 216 |
|
| 217 | + /** |
| 218 | + * Add a where clause for the given id(s) and language code to the query builder. |
| 219 | + * |
| 220 | + * @param QueryBuilder $builder The query builder. |
| 221 | + * @param string[]|string|null $mixIds One, none or many ids to use. |
| 222 | + * @param string|string[] $mixLangCode The language code/s to use, optional. |
| 223 | + * |
| 224 | + * @return void |
| 225 | + */ |
| 226 | + private function addWhere(QueryBuilder $builder, $mixIds, $mixLangCode = ''): void |
| 227 | + { |
| 228 | + $builder |
| 229 | + ->andWhere($builder->expr()->eq('att_id', ':attributeID')) |
| 230 | + ->setParameter(':attributeID', $this->get('id')); |
| 231 | + |
| 232 | + if (!empty($mixIds)) { |
| 233 | + if (\is_array($mixIds)) { |
| 234 | + $builder |
| 235 | + ->andWhere($builder->expr()->in('item_id', ':itemIDs')) |
| 236 | + ->setParameter('itemIDs', \array_map('intval', $mixIds), Connection::PARAM_INT_ARRAY); |
| 237 | + } else { |
| 238 | + $builder |
| 239 | + ->andWhere($builder->expr()->eq('item_id', ':itemID')) |
| 240 | + ->setParameter('itemID', $mixIds); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + if (!empty($mixLangCode)) { |
| 245 | + if (\is_array($mixLangCode)) { |
| 246 | + $builder |
| 247 | + ->andWhere($builder->expr()->in('langcode', ':langcodes')) |
| 248 | + ->setParameter('langcodes', \array_map('strval', $mixLangCode), Connection::PARAM_STR_ARRAY); |
| 249 | + } else { |
| 250 | + $builder |
| 251 | + ->andWhere($builder->expr()->eq('langcode', ':langcode')) |
| 252 | + ->setParameter('langcode', $mixLangCode); |
| 253 | + } |
| 254 | + } |
| 255 | + } |
| 256 | + |
213 | 257 | /** |
214 | 258 | * {@inheritdoc} |
215 | 259 | * |
@@ -420,39 +464,35 @@ protected function getSetValues($arrValue, $intId, $strLangCode) |
420 | 464 | */ |
421 | 465 | public function setTranslatedDataFor($arrValues, $strLangCode) |
422 | 466 | { |
423 | | - $database = $this->getMetaModel()->getServiceContainer()->getDatabase(); |
424 | 467 | // First off determine those to be updated and those to be inserted. |
425 | 468 | $valueIds = \array_keys($arrValues); |
426 | 469 | $existingIds = \array_keys($this->getTranslatedDataFor($valueIds, $strLangCode)); |
427 | 470 | $newIds = \array_diff($valueIds, $existingIds); |
428 | 471 |
|
429 | 472 | // Update existing values - delete if empty. |
430 | | - $queryUpdate = 'UPDATE ' . $this->getValueTable() . ' %s'; |
431 | | - $queryDelete = 'DELETE FROM ' . $this->getValueTable(); |
432 | | - |
433 | 473 | foreach ($existingIds as $existingId) { |
434 | | - $whereParts = $this->getWhere($existingId, $strLangCode); |
| 474 | + $builder = $this->connection->createQueryBuilder(); |
435 | 475 |
|
436 | 476 | if ($arrValues[$existingId]['value']['bin'][0]) { |
437 | | - $database->prepare($queryUpdate . ($whereParts ? ' WHERE ' . $whereParts['procedure'] : '')) |
438 | | - ->set($this->getSetValues($arrValues[$existingId], $existingId, $strLangCode)) |
439 | | - ->execute(($whereParts ? $whereParts['params'] : null)); |
| 477 | + $builder->update($this->getValueTable()); |
440 | 478 | } else { |
441 | | - $database->prepare($queryDelete . ($whereParts ? ' WHERE ' . $whereParts['procedure'] : '')) |
442 | | - ->execute(($whereParts ? $whereParts['params'] : null)); |
| 479 | + $builder->delete($this->getValueTable()); |
443 | 480 | } |
| 481 | + |
| 482 | + $this->addWhere($builder, $existingId, $strLangCode); |
| 483 | + $builder->execute(); |
444 | 484 | } |
445 | 485 |
|
446 | 486 | // Insert the new values - if not empty. |
447 | | - $queryInsert = 'INSERT INTO ' . $this->getValueTable() . ' %s'; |
448 | 487 | foreach ($newIds as $newId) { |
449 | 488 | if (!$arrValues[$newId]['value']['bin'][0]) { |
450 | 489 | continue; |
451 | 490 | } |
452 | 491 |
|
453 | | - $database->prepare($queryInsert) |
454 | | - ->set($this->getSetValues($arrValues[$newId], $newId, $strLangCode)) |
455 | | - ->execute(); |
| 492 | + $this->connection->insert( |
| 493 | + $this->getValueTable(), |
| 494 | + $this->getSetValues($arrValues[$newId], $newId, $strLangCode) |
| 495 | + ); |
456 | 496 | } |
457 | 497 | } |
458 | 498 |
|
|
0 commit comments