Skip to content

Commit fda985b

Browse files
committed
Use the doctrine dbal connection
1 parent ee0e0fc commit fda985b

File tree

2 files changed

+83
-42
lines changed

2 files changed

+83
-42
lines changed

src/Attribute/TranslatedFile.php

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Contao\System;
3636
use Contao\Validator;
3737
use Doctrine\DBAL\Connection;
38+
use Doctrine\DBAL\Query\QueryBuilder;
3839
use MetaModels\Attribute\TranslatedReference;
3940
use MetaModels\Helper\ToolboxFile;
4041
use MetaModels\IMetaModel;
@@ -178,6 +179,9 @@ protected function getValueTable()
178179
* @param string|string[] $mixLangCode The language code/s to use, optional.
179180
*
180181
* @return array
182+
*
183+
* @deprecated This is deprecated since 2.1 and where removed in 3.0.
184+
* Implement your own replacement for this.
181185
*/
182186
protected function getWhere($mixIds, $mixLangCode = '')
183187
{
@@ -210,6 +214,46 @@ protected function getWhere($mixIds, $mixLangCode = '')
210214
];
211215
}
212216

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+
213257
/**
214258
* {@inheritdoc}
215259
*
@@ -420,39 +464,35 @@ protected function getSetValues($arrValue, $intId, $strLangCode)
420464
*/
421465
public function setTranslatedDataFor($arrValues, $strLangCode)
422466
{
423-
$database = $this->getMetaModel()->getServiceContainer()->getDatabase();
424467
// First off determine those to be updated and those to be inserted.
425468
$valueIds = \array_keys($arrValues);
426469
$existingIds = \array_keys($this->getTranslatedDataFor($valueIds, $strLangCode));
427470
$newIds = \array_diff($valueIds, $existingIds);
428471

429472
// Update existing values - delete if empty.
430-
$queryUpdate = 'UPDATE ' . $this->getValueTable() . ' %s';
431-
$queryDelete = 'DELETE FROM ' . $this->getValueTable();
432-
433473
foreach ($existingIds as $existingId) {
434-
$whereParts = $this->getWhere($existingId, $strLangCode);
474+
$builder = $this->connection->createQueryBuilder();
435475

436476
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());
440478
} else {
441-
$database->prepare($queryDelete . ($whereParts ? ' WHERE ' . $whereParts['procedure'] : ''))
442-
->execute(($whereParts ? $whereParts['params'] : null));
479+
$builder->delete($this->getValueTable());
443480
}
481+
482+
$this->addWhere($builder, $existingId, $strLangCode);
483+
$builder->execute();
444484
}
445485

446486
// Insert the new values - if not empty.
447-
$queryInsert = 'INSERT INTO ' . $this->getValueTable() . ' %s';
448487
foreach ($newIds as $newId) {
449488
if (!$arrValues[$newId]['value']['bin'][0]) {
450489
continue;
451490
}
452491

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+
);
456496
}
457497
}
458498

src/Attribute/TranslatedFileOrder.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
namespace MetaModels\AttributeTranslatedFileBundle\Attribute;
2525

2626
use Contao\StringUtil;
27+
use Doctrine\DBAL\Connection;
28+
use Doctrine\DBAL\Query\QueryBuilder;
2729
use MetaModels\Attribute\IInternal;
2830
use MetaModels\Attribute\TranslatedReference;
2931
use MetaModels\Helper\ToolboxFile;
@@ -141,22 +143,20 @@ protected function getSetValues($arrValue, $intId, $strLangCode)
141143
*/
142144
public function setTranslatedDataFor($values, $langCode)
143145
{
144-
$database = $this->getMetaModel()->getServiceContainer()->getDatabase();
145146
// First off determine those to be updated and those to be inserted.
146147
$existingIds = \array_keys($this->getTranslatedDataFor(\array_keys($values), $langCode));
147148

148-
$queryUpdate = 'UPDATE ' . $this->getValueTable() . ' %s';
149149
foreach ($existingIds as $existingId) {
150150
if (!isset($values[$existingId]['value_sorting']['bin'][0])
151151
|| !\count(($setValues = $this->getSetValues($values[$existingId], $existingId, $langCode)))
152152
) {
153153
continue;
154154
}
155155

156-
$whereParts = $this->getWhere($existingId, $langCode);
157-
$database->prepare($queryUpdate . ($whereParts ? ' WHERE ' . $whereParts['procedure'] : ''))
158-
->set($setValues)
159-
->execute(($whereParts ? $whereParts['params'] : null));
156+
$builder = $this->connection->createQueryBuilder();
157+
$builder->update($setValues);
158+
$this->addWhere($builder, $existingId, $langCode);
159+
$builder->execute();
160160
}
161161
}
162162

@@ -176,41 +176,42 @@ public function getTranslatedDataFor($arrIds, $strLangCode)
176176
}
177177

178178
/**
179-
* Build a where clause for the given id(s) and language code.
179+
* Add a where clause for the given id(s) and language code to the query builder.
180180
*
181+
* @param QueryBuilder $builder The query builder.
181182
* @param string[]|string|null $mixIds One, none or many ids to use.
182183
* @param string|string[] $mixLangCode The language code/s to use, optional.
183184
*
184-
* @return array
185+
* @return void
185186
*/
186-
private function getWhere($mixIds, $mixLangCode = '')
187+
private function addWhere(QueryBuilder $builder, $mixIds, $mixLangCode = ''): void
187188
{
188-
$procedure = 'att_id=?';
189-
$parameters = [$this->get('id')];
189+
$builder
190+
->andWhere($builder->expr()->eq('att_id', ':attributeID'))
191+
->setParameter(':attributeID', $this->get('id'));
190192

191-
if (!empty($mixIds)) {
192-
if (\is_array($mixIds)) {
193-
$procedure .= ' AND item_id IN (' . $this->parameterMask($mixIds) . ')';
194-
$parameters = \array_merge($parameters, $mixIds);
193+
if (!empty($mixLangCode)) {
194+
if (\is_array($mixLangCode)) {
195+
$builder
196+
->andWhere($builder->expr()->in('langcode', ':langcodes'))
197+
->setParameter('langcodes', \array_map('strval', $mixLangCode), Connection::PARAM_STR_ARRAY);
195198
} else {
196-
$procedure .= ' AND item_id=?';
197-
$parameters[] = $mixIds;
199+
$builder
200+
->andWhere($builder->expr()->eq('langcode', ':langcode'))
201+
->setParameter('langcode', $mixLangCode);
198202
}
199203
}
200204

201-
if (!empty($mixLangCode)) {
202-
if (\is_array($mixLangCode)) {
203-
$procedure .= ' AND langcode IN (' . $this->parameterMask($mixLangCode) . ')';
204-
$parameters = \array_merge($parameters, $mixLangCode);
205+
if (!empty($mixIds)) {
206+
if (\is_array($mixIds)) {
207+
$builder
208+
->andWhere($builder->expr()->in('item_id', ':itemIDs'))
209+
->setParameter('itemIDs', \array_map('intval', $mixIds), Connection::PARAM_INT_ARRAY);
205210
} else {
206-
$procedure .= ' AND langcode=?';
207-
$parameters[] = $mixLangCode;
211+
$builder
212+
->andWhere($builder->expr()->eq('item_id', ':itemID'))
213+
->setParameter('itemID', $mixIds);
208214
}
209215
}
210-
211-
return [
212-
'procedure' => $procedure,
213-
'params' => $parameters
214-
];
215216
}
216217
}

0 commit comments

Comments
 (0)