Skip to content

Commit

Permalink
Fixes an SQL issue that could occur when applying Project Config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mmikkel committed Aug 21, 2020
1 parent a9a0066 commit 5d941c8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Cache Flag Changelog

## 1.2.3 - 2020-08-21

### Fixed

- Fixes an SQL issue that could occur when applying Project Config changes

## 1.2.2 - 2020-08-21

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mmikkel/cache-flag",
"description": "Cold template caches that can be flagged and automatically invalidated.",
"type": "craft-plugin",
"version": "1.2.2",
"version": "1.2.3",
"keywords": [
"craft",
"cms",
Expand Down
90 changes: 47 additions & 43 deletions src/services/ProjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,56 +29,59 @@ public function onProjectConfigChange(ConfigEvent $event)

$uid = $event->tokenMatches[0];

$id = (new Query())
$query = (new Query())
->select(['id'])
->from(Flags::tableName())
->where(['uid' => $uid])
->scalar();
->where(['uid' => $uid]);

$isNew = empty($id);
$source = \explode(':', $event->newValue['source']);
$sourceKey = $source[0] ?? null;
$sourceValue = $source[1] ?? null;

if ($isNew) {

$source = \explode(':', $event->newValue['source']);
$sourceKey = $source[0] ?? null;
$sourceValue = $source[1] ?? null;
if (!$sourceKey || !$sourceValue) {
return;
}

if (!$sourceKey || !$sourceValue) {
switch ($sourceKey) {
case 'section':
$column = 'sectionId';
$value = (int)Db::idByUid(Table::SECTIONS, $sourceValue);
break;
case 'categoryGroup':
$column = 'categoryGroupId';
$value = (int)Db::idByUid(Table::CATEGORYGROUPS, $sourceValue);
break;
case 'tagGroup':
$column = 'tagGroupId';
$value = (int)Db::idByUid(Table::TAGGROUPS, $sourceValue);
break;
case 'userGroup':
$column = 'userGroupId';
$value = (int)Db::idByUid(Table::USERGROUPS, $sourceValue);
break;
case 'volume':
$column = 'volumeId';
$value = (int)Db::idByUid(Table::VOLUMES, $sourceValue);
break;
case 'globalSet':
$column = 'globalSetId';
$value = (int)Db::idByUid(Table::GLOBALSETS, $sourceValue);
break;
case 'elementType':
$column = 'elementType';
$value = $sourceValue;
break;
default:
return;
}
}

switch ($sourceKey) {
case 'section':
$column = 'sectionId';
$value = (int)Db::idByUid(Table::SECTIONS, $sourceValue);
break;
case 'categoryGroup':
$column = 'categoryGroupId';
$value = (int)Db::idByUid(Table::CATEGORYGROUPS, $sourceValue);
break;
case 'tagGroup':
$column = 'tagGroupId';
$value = (int)Db::idByUid(Table::TAGGROUPS, $sourceValue);
break;
case 'userGroup':
$column = 'userGroupId';
$value = (int)Db::idByUid(Table::USERGROUPS, $sourceValue);
break;
case 'volume':
$column = 'volumeId';
$value = (int)Db::idByUid(Table::VOLUMES, $sourceValue);
break;
case 'globalSet':
$column = 'globalSetId';
$value = (int)Db::idByUid(Table::GLOBALSETS, $sourceValue);
break;
case 'elementType':
$column = 'elementType';
$value = $sourceValue;
break;
default:
return;
}
$query->orWhere([$column => $value]);

$id = $query->scalar();

$isNew = empty($id);

if ($isNew) {

$flags = $event->newValue['flags'];

Expand All @@ -95,6 +98,7 @@ public function onProjectConfigChange(ConfigEvent $event)
Craft::$app->db->createCommand()
->update(Flags::tableName(), [
'flags' => $event->newValue['flags'],
'uid' => $uid,
], ['id' => $id])
->execute();
}
Expand Down

0 comments on commit 5d941c8

Please sign in to comment.