@@ -140,156 +140,68 @@ public function getPrimaryKey($types = false)
140
140
return $ data ;
141
141
}
142
142
143
+
143
144
/**
144
- * Prepare the where, return an array with the following details:
145
- * 'where' => 'SQL Where Clause'
146
- * 'bindValues' => array with values for binding.
147
- * 'bindTypes' => array with types for binding.
148
- *
149
- * @param array $criteria Should be either
150
- * 'column' => 'value'
151
- * or:
152
- * 'column' => array('LIKE' => 'value')
153
- * 'column' => array('=' => 'value')
154
- * 'column' => array('>=' => 'value')
155
- * 'column' => array('<=' => 'value')
156
- * 'column' => array('>' => 'value')
157
- * 'column' => array('<' => 'value')
158
- * 'column' => array('IN' => array('value', 'value'))
159
- *
160
- * @param string|null $singleOperator Operator when doing a single criteria inline
161
- * @param string|null $singleValue Single or array value(s) when doing a single criteria inline
162
- *
163
- * @return array Where and bind result
164
- *
165
- * @throws \Exception Exceptions on error in the where criteria
145
+ * Start a query
146
+ * @return Query
166
147
*/
167
- private static function _prepareWhere ( $ criteria , $ singleOperator = null , $ singleValue = null )
148
+ public static function query ( )
168
149
{
169
- // Check parameter
170
- if (! is_array ($ criteria ) && ($ singleOperator == null || $ singleValue == null )) {
171
- return false ;
172
- }
173
-
174
- // Prepare returning result
175
- $ result = array (
176
- 'where ' => '' ,
177
- 'bindValues ' => array (),
178
- 'bindTypes ' => array ()
179
- );
150
+ return new Query (static ::class);
151
+ }
180
152
181
- // Map a single criteria to the normal array criteria.
182
- if (is_string ($ criteria ) && $ singleOperator !== null && $ singleValue !== null ) {
183
- $ criteria = array ($ criteria => array ($ singleOperator => $ singleValue ));
153
+ /**
154
+ * Find multiple entities by searching on the primary key values given
155
+ *
156
+ * @param array $ids Array of primary key values possible to return
157
+ * @return array<T>|Entity[]|false Array of entities or false on not found.
158
+ * @throws \Exception Exceptions are thrown when errors occur.
159
+ */
160
+ public static function findMany ($ ids )
161
+ {
162
+ if (! is_array ($ ids )) {
163
+ throw new \UnexpectedValueException ("IDs should be an array if primary key values! " );
184
164
}
185
165
186
- // First we will loop through the criteria and prepare the where clause
187
- $ where = "" ;
188
- $ bindValues = array ();
189
- $ bindTypes = array ();
190
-
191
- $ idx = 0 ;
192
- foreach ($ criteria as $ column => $ value ) {
193
- // Check for operators, if no operator, add it explicit
194
- if (!is_array ($ value )) {
195
- $ value = array ('= ' => $ value );
196
- }
197
-
198
- // Will contain [operator] => value
199
- $ operator = array_keys ($ value );
200
-
201
- // Few checks
202
- if (count ($ operator ) !== 1 ) {
203
- throw new \Exception ("The operator => value should contain only one operator, " . count ($ operator ) . " operators given for column " . $ column );
204
- }
205
-
206
- // Get the real operator and value
207
- $ operator = $ operator [0 ];
208
- $ value = $ value [$ operator ];
209
-
210
- // More checks for value
211
- if (is_array ($ value ) && $ operator !== 'IN ' ) {
212
- throw new \Exception ("Value is an array in the criteria of column " . $ column . ". Only IN operator allows arrays given as criteria value! " );
213
- }
214
- if ($ operator == 'IN ' && !is_array ($ value )) {
215
- throw new \Exception ("Value should be an array of values criteria of column " . $ column . ". Because you are using the IN operator! " );
216
- }
217
-
218
- // Adding basic where
219
- $ where .= "$ column " ;
220
-
221
- // The IN magic:
222
- if ($ operator == 'IN ' && is_array ($ value )) {
223
- $ where .= " IN ( " ;
224
-
225
- $ subIdx = 0 ;
226
-
227
- foreach ($ value as $ item => $ subValue ) {
228
- $ where .= "? " ;
229
- if (($ subIdx + 1 ) !== count ($ value )) {
230
- $ where .= ", " ;
231
- }
232
-
233
- $ bindValues [] = $ subValue ;
234
- $ bindTypes [] = is_int ($ subValue ) ? PDO ::PARAM_INT : PDO ::PARAM_STR ;
235
-
236
- $ subIdx ++;
237
- }
238
- $ where .= ") " ;
239
- } else {
240
- // None IN, just single where clause item.
241
- $ where .= "$ operator ? " ;
242
- $ bindValues [] = $ value ;
243
- $ bindTypes [] = is_int ($ value ) ? PDO ::PARAM_INT : PDO ::PARAM_STR ;
244
- }
245
-
246
- // If not the end of the criteria, then add AND to it.
247
- if (($ idx + 1 ) !== count ($ criteria )) {
248
- $ where .= " AND " ;
249
- }
250
-
251
-
252
- $ idx ++;
166
+ $ primaryKey = Structure::getTablePrimaryKey (static ::class);
167
+ if ($ primaryKey === false ) {
168
+ throw new \Exception ("Primary Key can't be detected! " );
253
169
}
170
+ // Only get column name for the primary key
171
+ $ primaryKey = $ primaryKey ->name ;
254
172
255
- $ result ['where ' ] = $ where ;
256
- $ result ['bindValues ' ] = $ bindValues ;
257
- $ result ['bindTypes ' ] = $ bindTypes ;
173
+ /** @var Entity[] $result */
174
+ $ result = static ::query ()->where (array ($ primaryKey => array ("IN " => $ ids )))->all ();
258
175
176
+ // Return results
259
177
return $ result ;
260
178
}
261
179
262
180
263
-
264
-
265
-
266
181
/**
267
- * Find entity by searching for the exact ID key.
182
+ * Find entity by searching for the exact ID key. Or create Query and return query.
268
183
*
269
- * @param string|int $id Primary key value
270
- * @return Entity|false
184
+ * @param string|int|null $id Primary key value, Ignore for query building.
185
+ * @return Entity|Query| false
271
186
*
272
187
* @throws \Exception
273
188
*/
274
- public static function find ($ id )
189
+ public static function find ($ id = null )
275
190
{
276
- $ primaryKey = Structure::getTablePrimaryKey (static ::class);
277
-
278
- if ($ primaryKey === false ) {
279
- throw new \Exception ("Primary Key can't be detected! " );
191
+ if ($ id === null ) {
192
+ return static ::query ();
280
193
}
281
- // Only get column name
282
- $ primaryKey = $ primaryKey ->name ;
283
194
284
- /** @var Entity $result */
285
- $ result = self ::getLink ()->fetchClass ("SELECT * FROM " . Structure::getTable (static ::class)->getFullTableName () . " WHERE $ primaryKey = :pkvalue " , array (':pkvalue ' => $ id ), array (), static ::class);
195
+ $ many = static ::findMany (array ($ id ));
286
196
287
- if ($ result instanceof Entity) {
288
- $ result ->_state = 1 ;
197
+ if (count ($ many ) <> 1 ) {
198
+ return false ;
199
+ }
200
+ $ result = $ many [0 ];
289
201
202
+ if ($ result instanceof Entity) {
290
203
return $ result ;
291
204
}
292
-
293
205
return false ;
294
206
}
295
207
@@ -314,29 +226,15 @@ public static function find($id)
314
226
*/
315
227
public static function findBy ($ criteria , $ operator = null , $ value = null )
316
228
{
317
- if (! is_array ($ criteria ) && ($ operator == null || $ value == null ))
229
+ if (! is_array ($ criteria ) && ($ operator == null && $ value == null ))
318
230
{
319
- throw new \UnexpectedValueException ("Criteria should be an array! " );
231
+ throw new \UnexpectedValueException ("Criteria should be an array! Or use the shorthand syntax " );
320
232
}
321
233
322
- // Prepare where statement
323
- $ preparedWhere = self ::_prepareWhere ($ criteria , $ operator , $ value );
324
-
325
- $ where = $ preparedWhere ['where ' ];
326
-
327
- $ sql = "SELECT * FROM " . Structure::getTable (static ::class)->getFullTableName () . " WHERE $ where " ;
328
- $ result = self ::getLink ()->fetchClass ($ sql , $ preparedWhere ['bindValues ' ], $ preparedWhere ['bindTypes ' ], static ::class);
329
-
330
- if ($ result instanceof Entity) {
331
- $ result ->_state = 1 ;
332
-
333
- return $ result ;
334
- }
335
-
336
- return false ;
234
+ // Return result
235
+ return static ::query ()->where ($ criteria , $ operator , $ value )->one ();
337
236
}
338
237
339
-
340
238
/**
341
239
* Insert or update the entity in the database
342
240
*
0 commit comments