|
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
|
|
|
* Attach like condition. |
|
27
|
|
|
* @param mixed $value |
|
28
|
|
|
* @param string $attribute |
|
29
|
|
|
* @param string|false $like false, 'like', 'or like', 'not like', 'or not like'. |
|
30
|
|
|
* @return $this |
|
31
|
|
|
*/ |
|
32
|
30 |
|
protected function likeCondition($value, $attribute, $like = false) |
|
33
|
|
|
{ |
|
34
|
30 |
|
if (!is_string($attribute) || empty($attribute)) { |
|
35
|
2 |
|
return $this; |
|
36
|
|
|
} |
|
37
|
30 |
|
if ($like !== false) { |
|
38
|
8 |
|
return $this->andWhere([$like, $attribute, $value]); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
30 |
|
return $this->andWhere([$attribute => $value]); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Specify range with $attribute to $query. |
|
45
|
|
|
* @param ActiveQuery $query |
|
46
|
|
|
* @param string $attribute |
|
47
|
|
|
* @param string $start |
|
48
|
|
|
* @param string $end |
|
49
|
|
|
* @return $query |
|
|
|
|
|
|
50
|
|
|
*/ |
|
51
|
4 |
|
protected static function range($query, $attribute, $start = null, $end = null) |
|
52
|
|
|
{ |
|
53
|
4 |
|
if (!empty($start)) { |
|
54
|
4 |
|
$query = $query->andWhere(['>=', $attribute, $start]); |
|
55
|
|
|
} |
|
56
|
4 |
|
if (!empty($end)) { |
|
57
|
4 |
|
$query = $query->andWhere(['<', $attribute, $end]); |
|
58
|
|
|
} |
|
59
|
4 |
|
return $query; |
|
60
|
|
|
} |
|
61
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.