-
Notifications
You must be signed in to change notification settings - Fork 8
/
TagQuery.php
54 lines (43 loc) · 1.57 KB
/
TagQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
* Tags for Craft CMS
*
* @link https://ethercreative.co.uk
* @copyright Copyright (c) 2019 Ether Creative
*/
namespace ether\tagManager\elements\db;
use Craft;
use ether\tagManager\TagManager;
use yii\db\Expression;
/**
* Class TagQuery
*
* @author Ether Creative
* @package ether\tagManager\elements\db
*/
class TagQuery extends \craft\elements\db\TagQuery
{
protected function afterPrepare (): bool
{
// due to the introduction of type filtering in element queries (see:https://github.com/craftcms/cms/discussions/9806)
// we have to make sure that the query is filtering based on 'craft\elements\Tag' and not 'ether\tagManager\elements\Tag'
for($i = 0; $i <= count($this->subQuery->where); $i++) {
if( !empty($this->subQuery->where[$i]['elements.type']) ) {
$this->subQuery->where[$i]['elements.type'] = 'craft\elements\Tag';
break;
}
}
if (Craft::$app->getDb()->getDriverName() === 'mysql')
return parent::afterPrepare();
if (!TagManager::getInstance()->getSettings()->enableUsage)
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->query->addSelect(new Expression('[[subquery.usage]] as [[usage]]'));
$this->subQuery->addSelect($getUsage);
return parent::afterPrepare();
}
}