Skip to content

Commit

Permalink
Fix issue with pgsql when usage column is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Oct 24, 2019
1 parent cb8038b commit 7f2773f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [Unreleased] 1.0.6
### Fixed
- Fix issue with pgsql when usage column is enabled

## 1.0.5 - 2019-06-26
### Added
- Add 'Usage' column to tags index table (#10)
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": "ether/tags",
"description": "A tag manager for Craft 3",
"type": "craft-plugin",
"version": "1.0.5",
"version": "1.0.6",
"keywords": [
"craft",
"cms",
Expand Down
11 changes: 7 additions & 4 deletions src/elements/db/TagQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@
class TagQuery extends \craft\elements\db\TagQuery
{

protected function beforePrepare (): bool
protected function afterPrepare (): bool
{
if (!TagManager::getInstance()->getSettings()->enableUsage)
return parent::beforePrepare();
return parent::afterPrepare();

if (count($this->query->select) === 1 && strtoupper($this->query->select[0]) === 'COUNT(*)')
return parent::afterPrepare();

$getUsage = new Expression(
'(SELECT COUNT(*) FROM (SELECT [[r.sourceId]], [[r.sourceSiteId]] FROM {{%relations}} r WHERE [[r.targetId]] = [[elements.id]] GROUP BY [[r.sourceId]], [[r.sourceSiteId]]) as usage) as [[usage]]'
);

$this->addSelect($getUsage);
$this->query->addSelect(new Expression('[[subquery.usage]] as [[usage]]'));
$this->subQuery->addSelect($getUsage);

return parent::beforePrepare();
return parent::afterPrepare();
}

}

0 comments on commit 7f2773f

Please sign in to comment.