1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii\db\conditions; |
4
|
|
|
|
5
|
|
|
use yii\db\ExpressionBuilderInterface; |
6
|
|
|
use yii\db\ExpressionBuilderTrait; |
7
|
|
|
use yii\db\ExpressionInterface; |
8
|
|
|
use yii\db\Query; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class BetweenColumnsConditionBuilder builds objects of [[BetweenColumnsCondition]] |
12
|
|
|
* |
13
|
|
|
* @author Dmytro Naumenko <[email protected]> |
14
|
|
|
* @since 2.0.14 |
15
|
|
|
*/ |
16
|
|
|
class BetweenColumnsConditionBuilder implements ExpressionBuilderInterface |
17
|
|
|
{ |
18
|
|
|
use ExpressionBuilderTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Method builds the raw SQL from the $expression that will not be additionally |
22
|
|
|
* escaped or quoted. |
23
|
|
|
* |
24
|
|
|
* @param ExpressionInterface|BetweenColumnsCondition $expression the expression to be built. |
25
|
|
|
* @param array $params the binding parameters. |
26
|
|
|
* @return string the raw SQL that will not be additionally escaped or quoted. |
27
|
|
|
*/ |
28
|
15 |
|
public function build(ExpressionInterface $expression, array &$params = []) |
29
|
|
|
{ |
30
|
15 |
|
$operator = $expression->getOperator(); |
|
|
|
|
31
|
|
|
|
32
|
15 |
|
$startColumn = $this->escapeColumnName($expression->getIntervalStartColumn(), $params); |
|
|
|
|
33
|
15 |
|
$endColumn = $this->escapeColumnName($expression->getIntervalEndColumn(), $params); |
|
|
|
|
34
|
15 |
|
$value = $this->createPlaceholder($expression->getValue(), $params); |
|
|
|
|
35
|
|
|
|
36
|
15 |
|
return "$value $operator $startColumn AND $endColumn"; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Prepares column name to be used in SQL statement. |
41
|
|
|
* |
42
|
|
|
* @param Query|ExpressionInterface|string $columnName |
43
|
|
|
* @param array $params the binding parameters. |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
15 |
|
protected function escapeColumnName($columnName, &$params = []) |
47
|
|
|
{ |
48
|
15 |
|
if ($columnName instanceof Query) { |
49
|
3 |
|
list($sql, $params) = $this->queryBuilder->build($columnName, $params); |
50
|
3 |
|
return "($sql)"; |
51
|
15 |
|
} elseif ($columnName instanceof ExpressionInterface) { |
52
|
|
|
return $this->queryBuilder->buildExpression($columnName, $params); |
53
|
15 |
|
} elseif (strpos($columnName, '(') === false) { |
54
|
15 |
|
return $this->queryBuilder->db->quoteColumnName($columnName); |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
return $columnName; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Attaches $value to $params array and returns placeholder. |
62
|
|
|
* |
63
|
|
|
* @param mixed $value |
64
|
|
|
* @param array $params passed by reference |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
15 |
|
protected function createPlaceholder($value, &$params) |
68
|
|
|
{ |
69
|
15 |
|
if ($value instanceof ExpressionInterface) { |
70
|
9 |
|
return $this->queryBuilder->buildExpression($value, $params); |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
return $this->queryBuilder->bindParam($value, $params); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: