|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/mangan |
|
7
|
|
|
* @licence AGPL or Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft |
|
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
|
11
|
|
|
* @link https://maslosoft.com/mangan/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Traits; |
|
15
|
|
|
|
|
16
|
|
|
use Maslosoft\Mangan\Criteria; |
|
17
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaInterface; |
|
18
|
|
|
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Attach criteria to model |
|
22
|
|
|
* |
|
23
|
|
|
* @see WithCriteriaInterface |
|
24
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
25
|
|
|
*/ |
|
26
|
|
|
trait WithCriteriaTrait |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Criteria |
|
31
|
|
|
* @var CriteriaInterface|Criteria |
|
32
|
|
|
*/ |
|
33
|
|
|
private $_criteria = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns the Criteria associated with this model. |
|
37
|
|
|
* @param boolean $createIfNull whether to create a criteria instance if it does not exist. Defaults to true. |
|
38
|
|
|
* @return CriteriaInterface|Criteria the query criteria that is associated with this model. |
|
39
|
|
|
* @since v1.0 |
|
40
|
|
|
* @Ignored |
|
41
|
|
|
*/ |
|
42
|
46 |
|
public function getDbCriteria($createIfNull = true) |
|
43
|
|
|
{ |
|
44
|
46 |
|
if ($this->_criteria === null) |
|
45
|
|
|
{ |
|
46
|
46 |
|
if ($createIfNull) |
|
47
|
|
|
{ |
|
48
|
46 |
|
$this->_criteria = new Criteria; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
46 |
|
return $this->_criteria; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set new criteria, previous criteria will be destroyed. |
|
56
|
|
|
* |
|
57
|
|
|
* @param CriteriaInterface|Criteria|array $criteria |
|
58
|
|
|
* @return static |
|
59
|
|
|
* @since v1.0 |
|
60
|
|
|
* @Ignored |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setDbCriteria($criteria) |
|
63
|
|
|
{ |
|
64
|
|
|
if (is_array($criteria)) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->_criteria = new Criteria($criteria); |
|
67
|
|
|
} |
|
68
|
|
|
else if ($criteria instanceof CriteriaInterface) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->_criteria = $criteria; |
|
71
|
|
|
} |
|
72
|
|
|
else |
|
73
|
|
|
{ |
|
74
|
|
|
$this->_criteria = new Criteria(); |
|
75
|
|
|
} |
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|