1 | <?php |
||
26 | class ScopeManager |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * Model instance |
||
31 | * @var AnnotatedInterface |
||
32 | */ |
||
33 | private $model = null; |
||
34 | |||
35 | /** |
||
36 | * |
||
37 | * @var CriteriaInterface |
||
38 | */ |
||
39 | private $criteria = null; |
||
40 | |||
41 | 89 | public function __construct($model) |
|
45 | |||
46 | /** |
||
47 | * Returns the declaration of named scopes. |
||
48 | * A named scope represents a query criteria that can be chained together with |
||
49 | * other named scopes and applied to a query. This method should be overridden |
||
50 | * by child classes to declare named scopes for the particular document classes. |
||
51 | * For example, the following code declares two named scopes: 'recently' and |
||
52 | * 'published'. |
||
53 | * <pre> |
||
54 | * return array( |
||
55 | * 'published'=>array( |
||
56 | * 'conditions'=>array( |
||
57 | * 'status'=>array('==', 1), |
||
58 | * ), |
||
59 | * ), |
||
60 | * 'recently'=>array( |
||
61 | * 'sort'=>array('create_time'=>Criteria::SortDesc), |
||
62 | * 'limit'=>5, |
||
63 | * ), |
||
64 | * ); |
||
65 | * </pre> |
||
66 | * If the above scopes are declared in a 'Post' model, we can perform the following |
||
67 | * queries: |
||
68 | * <pre> |
||
69 | * $posts=Post::model()->published()->findAll(); |
||
70 | * $posts=Post::model()->published()->recently()->findAll(); |
||
71 | * $posts=Post::model()->published()->published()->recently()->find(); |
||
72 | * </pre> |
||
73 | * |
||
74 | * @return array the scope definition. The array keys are scope names; the array |
||
75 | * values are the corresponding scope definitions. Each scope definition is represented |
||
76 | * as an array whose keys must be properties of {@link Criteria}. |
||
77 | * @since v1.0 |
||
78 | */ |
||
79 | public function scopes() |
||
83 | |||
84 | /** |
||
85 | * Returns the default named scope that should be implicitly applied to all queries for this model. |
||
86 | * Note, default scope only applies to SELECT queries. It is ignored for INSERT, UPDATE and DELETE queries. |
||
87 | * The default implementation simply returns an empty array. You may override this method |
||
88 | * if the model needs to be queried with some default criteria (e.g. only active records should be returned). |
||
89 | * @return array the mongo criteria. This will be used as the parameter to the constructor |
||
90 | * of {@link Criteria}. |
||
91 | * @since v1.2.2 |
||
92 | */ |
||
93 | public function defaultScope() |
||
97 | |||
98 | /** |
||
99 | * Resets all scopes and criteria applied including default scope. |
||
100 | * |
||
101 | * @return Document |
||
102 | * @since v1.0 |
||
103 | */ |
||
104 | public function resetScope() |
||
109 | |||
110 | /** |
||
111 | * Apply scopes to criteria, will create criteria object if not provided and pass it by reference |
||
112 | * @param CriteriaInterface|array|null $criteria |
||
113 | * @return CriteriaInterface |
||
114 | */ |
||
115 | 84 | public function apply(&$criteria = null) |
|
129 | |||
130 | public function reset() |
||
135 | |||
136 | 84 | private function getModelCriteria() |
|
153 | |||
154 | } |
||
155 |