@@ -355,23 +355,7 @@ public function whereOr(array $parameters)
355
355
if (count ($ parameters ) < 2 ) {
356
356
return $ this ->where ($ parameters );
357
357
}
358
- $ columns = [];
359
- $ values = [];
360
- foreach ($ parameters as $ key => $ val ) {
361
- if (is_int ($ key )) { // whereOr(['full condition'])
362
- $ columns [] = $ val ;
363
- } elseif (strpos ($ key , '? ' ) === false ) { // whereOr(['column1' => 1])
364
- $ columns [] = $ key . ' ? ' ;
365
- $ values [] = $ val ;
366
- } else { // whereOr(['column1 > ?' => 1])
367
- $ qNumber = substr_count ($ key , '? ' );
368
- if ($ qNumber > 1 && (!is_array ($ val ) || $ qNumber !== count ($ val ))) {
369
- throw new Nette \InvalidArgumentException ('Argument count does not match placeholder count. ' );
370
- }
371
- $ columns [] = $ key ;
372
- $ values = array_merge ($ values , $ qNumber > 1 ? $ val : [$ val ]);
373
- }
374
- }
358
+ [$ columns , $ values ] = $ this ->paramsOr ($ parameters );
375
359
$ columnsString = '( ' . implode (') OR ( ' , $ columns ) . ') ' ;
376
360
return $ this ->where ($ columnsString , $ values );
377
361
}
@@ -442,6 +426,19 @@ public function having(string $having, ...$params)
442
426
}
443
427
444
428
429
+ /**
430
+ * Sets having clause, more calls rewrite old value.
431
+ * @param array $parameters ['column1' => 1, 'column2 > ?' => 2, 'full condition']
432
+ * @return static
433
+ */
434
+ public function havingOr (array $ parameters )
435
+ {
436
+ [$ columns , $ values ] = $ this ->paramsOr ($ parameters );
437
+ $ columnsString = count ($ columns ) > 1 ? '( ' . implode (') OR ( ' , $ columns ) . ') ' : implode ('' , $ columns );
438
+ return $ this ->having ($ columnsString , $ values );
439
+ }
440
+
441
+
445
442
/**
446
443
* Aliases table. Example ':book:book_tag.tag', 'tg'
447
444
* @return static
@@ -756,6 +753,33 @@ public function getDataRefreshed(): bool
756
753
}
757
754
758
755
756
+ /**
757
+ * @param array $parameters ['column1' => 1, 'column2 > ?' => 2, 'full condition']
758
+ * @return array [$columns, $values] to be used with `where` or `having`
759
+ */
760
+ protected function paramsOr (array $ parameters ): array
761
+ {
762
+ $ columns = [];
763
+ $ values = [];
764
+ foreach ($ parameters as $ key => $ val ) {
765
+ if (is_int ($ key )) { // whereOr(['full condition'])
766
+ $ columns [] = $ val ;
767
+ } elseif (strpos ($ key , '? ' ) === FALSE ) { // whereOr(['column1' => 1])
768
+ $ columns [] = $ key . ' ? ' ;
769
+ $ values [] = $ val ;
770
+ } else { // whereOr(['column1 > ?' => 1])
771
+ $ qNumber = substr_count ($ key , '? ' );
772
+ if ($ qNumber > 1 && (!is_array ($ val ) || $ qNumber !== count ($ val ))) {
773
+ throw new Nette \InvalidArgumentException ('Argument count does not match placeholder count. ' );
774
+ }
775
+ $ columns [] = $ key ;
776
+ $ values = array_merge ($ values , $ qNumber > 1 ? $ val : [$ val ]);
777
+ }
778
+ }
779
+ return [$ columns , $ values ];
780
+ }
781
+
782
+
759
783
/********************* manipulation ****************d*g**/
760
784
761
785
0 commit comments