1 | <?php |
||
19 | abstract class AbstractScopeManager implements ModelAwareInterface |
||
20 | { |
||
21 | |||
22 | use ModelAwareTrait; |
||
23 | |||
24 | /** |
||
25 | * |
||
26 | * @var CriteriaInterface |
||
27 | */ |
||
28 | private $criteria = null; |
||
29 | |||
30 | /** |
||
31 | * Returns the declaration of named scopes. |
||
32 | * A named scope represents a query criteria that can be chained together with |
||
33 | * other named scopes and applied to a query. This method should be overridden |
||
34 | * by child classes to declare named scopes for the particular document classes. |
||
35 | * For example, the following code declares two named scopes: 'recently' and |
||
36 | * 'published'. |
||
37 | * <pre> |
||
38 | * return array( |
||
39 | * 'published'=>array( |
||
40 | * 'conditions'=>array( |
||
41 | * 'status'=>array('==', 1), |
||
42 | * ), |
||
43 | * ), |
||
44 | * 'recently'=>array( |
||
45 | * 'sort'=>array('create_time'=>Criteria::SortDesc), |
||
46 | * 'limit'=>5, |
||
47 | * ), |
||
48 | * ); |
||
49 | * </pre> |
||
50 | * If the above scopes are declared in a 'Post' model, we can perform the following |
||
51 | * queries: |
||
52 | * <pre> |
||
53 | * $posts=Post::model()->published()->findAll(); |
||
54 | * $posts=Post::model()->published()->recently()->findAll(); |
||
55 | * $posts=Post::model()->published()->published()->recently()->find(); |
||
56 | * </pre> |
||
57 | * |
||
58 | * @return array the scope definition. The array keys are scope names; the array |
||
59 | * values are the corresponding scope definitions. Each scope definition is represented |
||
60 | * as an array whose keys must be properties of {@link Criteria}. |
||
61 | * @since v1.0 |
||
62 | */ |
||
63 | public function scopes() |
||
67 | |||
68 | /** |
||
69 | * Returns the default named scope that should be implicitly applied to all queries for this model. |
||
70 | * Note, default scope only applies to SELECT queries. It is ignored for INSERT, UPDATE and DELETE queries. |
||
71 | * The default implementation simply returns an empty array. You may override this method |
||
72 | * if the model needs to be queried with some default criteria (e.g. only active records should be returned). |
||
73 | * @return array the mongo criteria. This will be used as the parameter to the constructor |
||
74 | * of {@link Criteria}. |
||
75 | * @since v1.2.2 |
||
76 | */ |
||
77 | public function defaultScope() |
||
81 | |||
82 | /** |
||
83 | * Resets all scopes and criteria applied including default scope. |
||
84 | * |
||
85 | * @return Document |
||
86 | * @since v1.0 |
||
87 | */ |
||
88 | public function resetScope() |
||
93 | |||
94 | /** |
||
95 | * Apply scopes to criteria, will create criteria object if not provided and pass it by reference |
||
96 | * @param CriteriaInterface|array|null $criteria |
||
97 | * @return CriteriaInterface |
||
98 | */ |
||
99 | 86 | public function apply(&$criteria = null) |
|
113 | |||
114 | public function reset() |
||
119 | |||
120 | 86 | protected function getModelCriteria() |
|
137 | |||
138 | abstract protected function getNewCriteria($criteria = null); |
||
139 | } |
||
140 |