1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
6
|
|
|
* @link https://www.flipboxfactory.com/patron/domains/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\patron\queries; |
10
|
|
|
|
11
|
|
|
use craft\helpers\Db; |
12
|
|
|
use flipbox\patron\records\Token; |
13
|
|
|
use flipbox\patron\records\TokenEnvironment; |
14
|
|
|
use yii\db\Expression; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
trait TokenEnvironmentAttributeTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string|string[]|null The environment(s). Prefix with "not " to exclude them. |
24
|
|
|
*/ |
25
|
|
|
public $environment; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Adds an additional WHERE condition to the existing one. |
29
|
|
|
* The new condition and the existing one will be joined using the `AND` operator. |
30
|
|
|
* @param string|array|Expression $condition the new WHERE condition. Please refer to [[where()]] |
31
|
|
|
* on how to specify this parameter. |
32
|
|
|
* @param array $params the parameters (name => value) to be bound to the query. |
33
|
|
|
* @return $this the query object itself |
34
|
|
|
* @see where() |
35
|
|
|
* @see orWhere() |
36
|
|
|
*/ |
37
|
|
|
abstract public function andWhere($condition, $params = []); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Appends a LEFT OUTER JOIN part to the query. |
41
|
|
|
* @param string|array $table the table to be joined. |
42
|
|
|
* |
43
|
|
|
* Use a string to represent the name of the table to be joined. |
44
|
|
|
* The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). |
45
|
|
|
* The method will automatically quote the table name unless it contains some parenthesis |
46
|
|
|
* (which means the table is given as a sub-query or DB expression). |
47
|
|
|
* |
48
|
|
|
* Use an array to represent joining with a sub-query. The array must contain only one element. |
49
|
|
|
* The value must be a [[Query]] object representing the sub-query while the corresponding key |
50
|
|
|
* represents the alias for the sub-query. |
51
|
|
|
* |
52
|
|
|
* @param string|array $on the join condition that should appear in the ON part. |
53
|
|
|
* Please refer to [[join()]] on how to specify this parameter. |
54
|
|
|
* @param array $params the parameters (name => value) to be bound to the query |
55
|
|
|
* @return $this the query object itself |
56
|
|
|
*/ |
57
|
|
|
abstract public function leftJoin($table, $on = '', $params = []); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $environment |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function environment($environment) |
64
|
|
|
{ |
65
|
|
|
$this->environment = $environment; |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Apply environment params |
71
|
|
|
*/ |
72
|
|
|
protected function applyEnvironmentConditions() |
73
|
|
|
{ |
74
|
|
|
if (empty($this->environment)) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$alias = TokenEnvironment::tableAlias(); |
79
|
|
|
|
80
|
|
|
$this->leftJoin( |
81
|
|
|
TokenEnvironment::tableName() . ' ' . $alias, |
82
|
|
|
'[[' . $alias . '.tokenId]] = [[' . Token::tableAlias() . '.id]]' |
83
|
|
|
); |
84
|
|
|
$this->andWhere( |
85
|
|
|
Db::parseParam($alias . '.environment', $this->environment) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|