1 | <?php |
||
31 | abstract class AbstractScopeManager implements ModelAwareInterface |
||
32 | { |
||
33 | |||
34 | use ModelAwareTrait; |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | * @var CriteriaInterface |
||
39 | */ |
||
40 | private $criteria = null; |
||
41 | |||
42 | /** |
||
43 | * Returns the declaration of named scopes. |
||
44 | * A named scope represents a query criteria that can be chained together with |
||
45 | * other named scopes and applied to a query. This method should be overridden |
||
46 | * by child classes to declare named scopes for the particular document classes. |
||
47 | * For example, the following code declares two named scopes: 'recently' and |
||
48 | * 'published'. |
||
49 | * <pre> |
||
50 | * return array( |
||
51 | * 'published'=>array( |
||
52 | * 'conditions'=>array( |
||
53 | * 'status'=>array('==', 1), |
||
54 | * ), |
||
55 | * ), |
||
56 | * 'recently'=>array( |
||
57 | * 'sort'=>array('create_time'=>Criteria::SortDesc), |
||
58 | * 'limit'=>5, |
||
59 | * ), |
||
60 | * ); |
||
61 | * </pre> |
||
62 | * If the above scopes are declared in a 'Post' model, we can perform the following |
||
63 | * queries: |
||
64 | * <pre> |
||
65 | * $posts=Post::model()->published()->findAll(); |
||
66 | * $posts=Post::model()->published()->recently()->findAll(); |
||
67 | * $posts=Post::model()->published()->published()->recently()->find(); |
||
68 | * </pre> |
||
69 | * |
||
70 | * @return array the scope definition. The array keys are scope names; the array |
||
71 | * values are the corresponding scope definitions. Each scope definition is represented |
||
72 | * as an array whose keys must be properties of {@link Criteria}. |
||
73 | * @since v1.0 |
||
74 | */ |
||
75 | public function scopes() |
||
79 | |||
80 | /** |
||
81 | * Returns the default named scope that should be implicitly applied to all queries for this model. |
||
82 | * Note, default scope only applies to SELECT queries. It is ignored for INSERT, UPDATE and DELETE queries. |
||
83 | * The default implementation simply returns an empty array. You may override this method |
||
84 | * if the model needs to be queried with some default criteria (e.g. only active records should be returned). |
||
85 | * @return array the mongo criteria. This will be used as the parameter to the constructor |
||
86 | * of {@link Criteria}. |
||
87 | * @since v1.2.2 |
||
88 | */ |
||
89 | public function defaultScope() |
||
93 | |||
94 | /** |
||
95 | * Resets all scopes and criteria applied including default scope. |
||
96 | * |
||
97 | * @return Document |
||
98 | * @since v1.0 |
||
99 | */ |
||
100 | public function resetScope() |
||
105 | |||
106 | /** |
||
107 | * Apply scopes to criteria, will create criteria object if not provided and pass it by reference |
||
108 | * @param CriteriaInterface|array|null $criteria |
||
109 | * @return CriteriaInterface |
||
110 | */ |
||
111 | 97 | public function apply(&$criteria = null) |
|
112 | { |
||
113 | 97 | if (null === $criteria) |
|
114 | { |
||
115 | 24 | return $this->getModelCriteria(); |
|
116 | } |
||
117 | 92 | elseif (is_array($criteria)) |
|
118 | { |
||
119 | 1 | $criteria = $this->getNewCriteria($criteria); |
|
120 | } |
||
121 | 92 | $criteria->mergeWith($this->criteria); |
|
122 | 92 | $criteria->mergeWith($this->getModelCriteria()); |
|
123 | 92 | if ($criteria instanceof DecoratableInterface) |
|
124 | { |
||
125 | 92 | $criteria->decorateWith($this->getModel()); |
|
126 | } |
||
127 | 92 | return $criteria; |
|
128 | } |
||
129 | |||
130 | public function reset() |
||
135 | |||
136 | 97 | protected function getModelCriteria() |
|
153 | |||
154 | abstract public function getNewCriteria($criteria = null); |
||
155 | } |
||
156 |