]> _ Git - cubist_cms-back.git/commitdiff
Fix logic issue that occurred when combining `whereVariant` with other `where` clause...
authorStephen Cameron <stephen@cubedesigners.com>
Fri, 4 Feb 2022 21:54:42 +0000 (22:54 +0100)
committerStephen Cameron <stephen@cubedesigners.com>
Fri, 4 Feb 2022 21:54:42 +0000 (22:54 +0100)
src/app/Magic/Models/CMSPage.php
src/app/Magic/QueryBuilder.php

index 0c824dffb1cb9f68fa83a41308d6377d67749f67..f18d6876ffcdf410b5b9eb236e554c63de25d701 100644 (file)
@@ -226,7 +226,7 @@ class CMSPage extends CubistMagicNestedModel
             try {
                 $r = DB::table(self::$_table)->orderBy('lft');
                 if (App::hasVariant()) {
-                    $r->whereRaw('variant IS NULL OR JSON_CONTAINS(variant, \'["' . $variant . '"]\')');
+                    $r->whereRaw('(variant IS NULL OR JSON_CONTAINS(variant, \'["' . $variant . '"]\'))');
                 }
                 static::$_pagesList[$variant] = $r->get()->pluck('name', 'id');
             } catch (Exception $e) {
index ea6c0b50bd7a01e947bcd1590b923e48aacacd06..9386c36dd5a57c8639cf107bcec6b7efb8c7bdf6 100644 (file)
@@ -13,7 +13,9 @@ class QueryBuilder extends Builder
             $variant = App::getVariant();
         }
         if (App::hasVariant()) {
-            return $this->whereRaw('variant IS NULL OR JSON_CONTAINS(variant, \'["' . $variant . '"]\')');
+            // Raw WHERE statement containing an OR clause must be wrapped in parentheses to contain the logic.
+            // Otherwise, it will cause unexpected results when used with other WHERE statements in the QueryBuilder
+            return $this->whereRaw('(variant IS NULL OR JSON_CONTAINS(variant, \'["' . $variant . '"]\'))');
         }
         return $this;
     }