@@ -139,7 +139,7 @@ public function getTimestampHash($timestamp_field)
139139 }
140140
141141 $ table_name = $ this ->getTableName ();
142- $ conditions = $ this ->conditions ? " WHERE $ this ->conditions " : '' ;
142+ $ conditions = $ this ->getConditions () ? " WHERE { $ this ->getConditions ()} " : '' ;
143143
144144 if ($ this ->count () > 0 ) {
145145 if ($ join_expression = $ this ->getJoinExpression ()) {
@@ -238,7 +238,7 @@ private function getSelectSql($all_fields = true)
238238
239239 $ fields = $ all_fields ? '* ' : '`id` ' ;
240240 $ table_name = $ this ->connection ->escapeTableName ($ this ->getTableName ());
241- $ conditions = $ this ->conditions ? "WHERE $ this ->conditions " : '' ;
241+ $ conditions = $ this ->getConditions () ? "WHERE { $ this ->getConditions ()} " : '' ;
242242
243243 if ($ order_by = $ this ->getOrderBy ()) {
244244 $ order_by = "ORDER BY $ this ->order_by " ;
@@ -267,7 +267,7 @@ public function count()
267267 }
268268
269269 $ table_name = $ this ->connection ->escapeTableName ($ this ->getTableName ());
270- $ conditions = $ this ->conditions ? " WHERE $ this ->conditions " : '' ;
270+ $ conditions = $ this ->getConditions () ? " WHERE { $ this ->getConditions ()} " : '' ;
271271
272272 if ($ join_expression = $ this ->getJoinExpression ()) {
273273 return (integer ) $ this ->connection ->executeFirstCell ("SELECT COUNT( $ table_name.`id`) FROM $ table_name $ join_expression $ conditions " );
@@ -340,7 +340,16 @@ public function &orderBy($value)
340340 *
341341 * @var string
342342 */
343- private $ conditions ;
343+ private $ conditions = [];
344+
345+ /**
346+ * Cached conditions as string.
347+ *
348+ * Call to where() resets this value to false, and getConditions() rebuilds it if it FALSE on request.
349+ *
350+ * @var string|bool
351+ */
352+ private $ conditions_as_string = false ;
344353
345354 /**
346355 * Return conditions.
@@ -349,27 +358,52 @@ public function &orderBy($value)
349358 */
350359 public function getConditions ()
351360 {
352- return $ this ->conditions ;
361+ if ($ this ->conditions_as_string === false ) {
362+ switch (count ($ this ->conditions )) {
363+ case 0 :
364+ $ this ->conditions_as_string = '' ;
365+ break ;
366+ case 1 :
367+ $ this ->conditions_as_string = $ this ->conditions [0 ];
368+ break ;
369+ default :
370+ $ this ->conditions_as_string = implode (' AND ' , array_map (function ($ condition ) {
371+ return "( $ condition) " ;
372+ }, $ this ->conditions ));
373+ }
374+ }
375+
376+ return $ this ->conditions_as_string ;
353377 }
354378
355379 /**
356380 * Set collection conditions.
357381 *
358- * @param mixed ...$arguments
382+ * @param string $pattern
383+ * @param array $arguments
359384 * @return $this
360385 */
361- public function &where ()
386+ public function &where ($ pattern , ... $ arguments )
362387 {
363- $ number_of_arguments = func_num_args ();
388+ if (empty ($ pattern )) {
389+ throw new InvalidArgumentException ('Pattern argument is required ' );
390+ }
364391
365- if ($ number_of_arguments === 1 ) {
366- $ this ->conditions = $ this ->connection ->prepareConditions (func_get_arg (0 ));
367- } elseif ($ number_of_arguments > 1 ) {
368- $ this ->conditions = $ this ->connection ->prepareConditions (func_get_args ());
392+ if (is_string ($ pattern )) {
393+ $ this ->conditions [] = $ this ->connection ->prepareConditions (array_merge ([$ pattern ], $ arguments ));
394+ } elseif (is_array ($ pattern )) {
395+ if (!empty ($ arguments )) {
396+ throw new LogicException ('When pattern is an array, no extra arguments are allowed ' );
397+ }
398+
399+ $ this ->conditions [] = $ this ->connection ->prepareConditions ($ pattern );
369400 } else {
370- throw new InvalidArgumentException ('At least one argument expected ' );
401+ throw new InvalidArgumentException ('Pattern can be string or an array ' );
371402 }
372403
404+ // Force rebuild of conditions as string on next getConditions() call
405+ $ this ->conditions_as_string = false ;
406+
373407 return $ this ;
374408 }
375409
0 commit comments