1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
4
|
|
|
* @license https://github.com/flipboxfactory/craft-sortable-associations/blob/master/LICENSE |
5
|
|
|
* @link https://github.com/flipboxfactory/craft-sortable-associations |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace flipbox\craft\domains\fields; |
9
|
|
|
|
10
|
|
|
use craft\elements\db\ElementQuery; |
11
|
|
|
use craft\elements\db\ElementQueryInterface; |
12
|
|
|
use craft\helpers\Db; |
13
|
|
|
use flipbox\craft\domains\records\Domain; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Flipbox Factory <[email protected]> |
17
|
|
|
* @since 1.0.0 |
18
|
|
|
* |
19
|
|
|
* @property int $id |
20
|
|
|
*/ |
21
|
|
|
trait ModifyElementsQueryTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
|
|
public function modifyElementsQuery( |
27
|
|
|
ElementQueryInterface $query, |
28
|
|
|
$value |
29
|
|
|
) { |
30
|
|
|
if ($value === null || !$query instanceof ElementQuery) { |
31
|
|
|
return null; |
32
|
|
|
} |
33
|
|
|
if ($value === false) { |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
if (is_string($value)) { |
37
|
|
|
$this->modifyElementsQueryForStringValue($query, $value); |
38
|
|
|
return null; |
39
|
|
|
} |
40
|
|
|
$this->modifyElementsQueryForTargetValue($query, $value); |
41
|
|
|
return null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ElementQuery $query |
46
|
|
|
* @param string $value |
47
|
|
|
*/ |
48
|
|
|
protected function modifyElementsQueryForStringValue( |
49
|
|
|
ElementQuery $query, |
50
|
|
|
string $value |
51
|
|
|
) { |
52
|
|
|
if ($value === 'not :empty:') { |
53
|
|
|
$value = ':notempty:'; |
54
|
|
|
} |
55
|
|
|
if ($value === ':notempty:' || $value === ':empty:') { |
56
|
|
|
$this->modifyElementsQueryForEmptyValue($query, $value); |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
$this->modifyElementsQueryForTargetValue($query, $value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param ElementQuery $query |
64
|
|
|
* @param $value |
65
|
|
|
*/ |
66
|
|
|
protected function modifyElementsQueryForTargetValue( |
67
|
|
|
ElementQuery $query, |
68
|
|
|
$value |
69
|
|
|
) { |
70
|
|
|
$alias = Domain::tableAlias(); |
71
|
|
|
$name = Domain::tableName(); |
72
|
|
|
|
73
|
|
|
$joinTable = "{$name} {$alias}"; |
74
|
|
|
$query->query->innerJoin($joinTable, "[[{$alias}.elementId]] = [[subquery.elementsId]]"); |
75
|
|
|
$query->subQuery->innerJoin($joinTable, "[[{$alias}.elementId]] = [[elements.id]]"); |
76
|
|
|
$query->subQuery->andWhere( |
77
|
|
|
Db::parseParam($alias . '.fieldId', $this->id) |
78
|
|
|
); |
79
|
|
|
$query->subQuery->andWhere( |
80
|
|
|
Db::parseParam($alias . '.domain', $value) |
81
|
|
|
); |
82
|
|
|
$query->query->distinct(true); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param ElementQuery $query |
87
|
|
|
* @param string $value |
88
|
|
|
*/ |
89
|
|
|
protected function modifyElementsQueryForEmptyValue( |
90
|
|
|
ElementQuery $query, |
91
|
|
|
string $value |
92
|
|
|
) { |
93
|
|
|
/** @var string $operator */ |
94
|
|
|
$operator = ($value === ':notempty:' ? '!=' : '='); |
95
|
|
|
|
96
|
|
|
$query->subQuery->andWhere( |
97
|
|
|
$this->emptyValueSubSelect( |
|
|
|
|
98
|
|
|
Domain::tableAlias(), |
99
|
|
|
Domain::tableName(), |
100
|
|
|
$operator |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $alias |
107
|
|
|
* @param string $name |
108
|
|
|
* @param string $operator |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
protected function emptyValueSubSelect( |
112
|
|
|
string $alias, |
113
|
|
|
string $name, |
114
|
|
|
string $operator |
115
|
|
|
): string { |
116
|
|
|
return "(select count([[{$alias}.elementId]]) from " . |
117
|
|
|
$name . |
118
|
|
|
" {{{$alias}}} where [[{$alias}.elementId" . |
119
|
|
|
"]] = [[elements.id]] and [[{$alias}.fieldId]] = {$this->id}) {$operator} 0"; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: