1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.me/ |
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
10
|
|
|
* @license https://vistart.me/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace rhosocial\base\models\traits; |
14
|
|
|
|
15
|
|
|
use yii\db\ActiveQuery; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This trait attach two base conditions. |
19
|
|
|
* |
20
|
|
|
* @version 1.0 |
21
|
|
|
* @author vistart <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
trait QueryTrait |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Attach like condition. |
28
|
|
|
* @param mixed $value |
29
|
|
|
* @param string $attribute |
30
|
|
|
* @param string|false $like false, 'like', 'or like', 'not like', 'or not like'. |
31
|
|
|
* @return $this |
32
|
|
|
*/ |
33
|
10 |
|
protected function likeCondition($value, $attribute, $like = false) |
34
|
|
|
{ |
35
|
10 |
|
if (!is_string($attribute) || empty($attribute)) { |
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
10 |
|
if ($like) { |
|
|
|
|
39
|
1 |
|
return $this->andWhere([$like, $attribute, $value]); |
|
|
|
|
40
|
|
|
} |
41
|
10 |
|
return $this->andWhere([$attribute => $value]); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Specify range with $attribute to $query. |
46
|
|
|
* @param ActiveQuery $query |
47
|
|
|
* @param string $attribute |
48
|
|
|
* @param string $start |
49
|
|
|
* @param string $end |
50
|
|
|
* @return $query |
|
|
|
|
51
|
|
|
*/ |
52
|
|
|
protected static function range($query, $attribute, $start = null, $end = null) |
53
|
|
|
{ |
54
|
|
|
if (!empty($start)) { |
55
|
|
|
$query = $query->andWhere(['>=', $attribute, $start]); |
56
|
|
|
} |
57
|
|
|
if (!empty($end)) { |
58
|
|
|
$query = $query->andWhere(['<', $attribute, $end]); |
59
|
|
|
} |
60
|
|
|
return $query; |
61
|
|
|
} |
62
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: