@@ -50,6 +50,14 @@ class Criteria
5050 * @var array Parameters for the prepared statement binding.
5151 */
5252 public $ PreparedParameters ;
53+ /**
54+ * @var array A list of [field, bool] tuples, where the second item determines whether the order should be ascending.
55+ */
56+ public $ OrderingFields ;
57+ /**
58+ * @var array The [int, int] array representing the LIMIT clause, where the first item is the offset and the second is the count.
59+ */
60+ public $ Limit ;
5361
5462 /**
5563 * @param string $baseEntityClass
@@ -73,6 +81,84 @@ public function add($expression)
7381 $ this ->expressions []=$ expression ;
7482 }
7583
84+ /**
85+ * @param string $field The field on which to order ascendingly.
86+ * @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
87+ */
88+ public function orderBy ($ field )
89+ {
90+ if ($ this ->OrderingFields !==null ){
91+ throw new Exception ('The orderBy has already been called. Call thenBy instead. ' );
92+ }
93+ $ this ->OrderingFields =[];
94+ $ this ->addOrdering ($ field , true );
95+ return $ this ;
96+ }
97+
98+ /**
99+ * @param string $field The field on which to order descendingly.
100+ * @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
101+ */
102+ public function orderByDescending ($ field )
103+ {
104+ if ($ this ->OrderingFields !==null ){
105+ throw new Exception ('The orderBy has already been called. Call thenByDescending instead. ' );
106+ }
107+ $ this ->OrderingFields =[];
108+ $ this ->addOrdering ($ field , false );
109+ return $ this ;
110+ }
111+
112+ /**
113+ * @param string $field The field on which to order ascendingly.
114+ * @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
115+ */
116+ public function thenBy ($ field )
117+ {
118+ if ($ this ->OrderingFields ===null ){
119+ throw new Exception ('The orderBy has not yet been called. Call orderBy instead. ' );
120+ }
121+ $ this ->addOrdering ($ field , true );
122+ return $ this ;
123+ }
124+
125+ /**
126+ * @param string $field The field on which to order descendingly.
127+ * @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
128+ */
129+ public function thenByDescending ($ field )
130+ {
131+ if ($ this ->OrderingFields ===null ){
132+ throw new Exception ('The orderBy has not yet been called. Call orderByDescending instead. ' );
133+ }
134+ $ this ->addOrdering ($ field , false );
135+ return $ this ;
136+ }
137+
138+ /**
139+ * @param string $field
140+ * @param bool $ascending
141+ */
142+ private function addOrdering ($ field ,$ ascending )
143+ {
144+ if ($ field ===null ){
145+ throw new Exception ('Field for order by must not be null. ' );
146+ }
147+ $ this ->OrderingFields []=[$ field ,$ ascending ];
148+ }
149+
150+ /**
151+ * @param int $count Specifies the maximum number of rows to be returned.
152+ * @param int $offset Specifies the offset of the first row to be returned.
153+ */
154+ public function limit ($ count , $ offset =0 )
155+ {
156+ if ($ this ->Limit !==null ){
157+ throw new Exception ('Limit was already set. ' );
158+ }
159+ $ this ->Limit =[$ offset ,$ count ];
160+ }
161+
76162 public function prepare ()
77163 {
78164 // Joins
0 commit comments