Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Update MultiFormObjectDecorator to be SS3.2 compatible #10

Open
halkyon opened this Issue Jan 7, 2013 · 1 comment

Comments

Projects
None yet
2 participants
Owner

halkyon commented Jan 7, 2013

I've fixed it to extend from DataExtension, so that i18nTextCollector would run, but the class needs to be fixed to work with the SQLQuery API in SilverStripe 3.x, as it still checks protected variables instead of using getWhere() etc.

Owner

chillu commented Apr 30, 2015

Puah, just had a crack at this for 3.2.x - tricky. The addWhere() call duplicates an existing one, and its not easy to update that filter given the SQLSelect API.

SELECT DISTINCT "MultiFormObjectDecorator_DataObject"."ClassName", "MultiFormObjectDecorator_DataObject"."LastEdited", "MultiFormObjectDecorator_DataObject"."Created", "MultiFormObjectDecorator_DataObject"."Name", "MultiFormObjectDecorator_DataObject"."MultiFormIsTemporary", "MultiFormObjectDecorator_DataObject"."MultiFormSessionID", "MultiFormObjectDecorator_DataObject"."ID",
            CASE WHEN "MultiFormObjectDecorator_DataObject"."ClassName" IS NOT NULL THEN "MultiFormObjectDecorator_DataObject"."ClassName"
            ELSE 'MultiFormObjectDecorator_DataObject' END AS "RecordClassName"
 FROM "MultiFormObjectDecorator_DataObject"
 WHERE ("MultiFormObjectDecorator_DataObject"."MultiFormIsTemporary" = ?)
 AND ("MultiFormObjectDecorator_DataObject".MultiFormIsTemporary = 0)
diff --git a/.travis.yml b/.travis.yml
index 86d952b..9840a57 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -8,19 +8,18 @@ php:
 env:
   global:
     - CORE_RELEASE=master
-  matrix:
-    - DB=MYSQL
-    - DB=SQLITE
-    - DB=PGSQL

 matrix:
   include:
-    - php: 5.5
-      env: DB=MYSQL
-    - php: 5.6
-      env: DB=MYSQL
     - php: 5.3
       env: DB=MYSQL
+    - php: 5.4
+      env: DB=MYSQL
+    - php: 5.5
+      env: DB=PGSQL
+    - php: 5.6
+      env: DB=SQLITE
+    

 before_script:
  - composer self-update
diff --git a/code/extensions/MultiFormObjectDecorator.php b/code/extensions/MultiFormObjectDecorator.php
index b9e5e12..ac445cb 100644
--- a/code/extensions/MultiFormObjectDecorator.php
+++ b/code/extensions/MultiFormObjectDecorator.php
@@ -36,12 +36,7 @@ class MultiFormObjectDecorator extends DataExtension {
            return;
        }

-       if(
-           strpos($where[0], ".`ID` = ") === false 
-           && strpos($where[0], ".ID = ") === false 
-           && strpos($where[0], "ID = ") !== 0
-           && !$this->wantsTemporary($query)
-       ) {
+       if(!$query->filtersOnID() && !$this->wantsTemporary($query)) {
            $from = array_values($query->getFrom());
            $query->addWhere("{$from[0]}.MultiFormIsTemporary = 0");
        }
@@ -56,11 +51,27 @@ class MultiFormObjectDecorator extends DataExtension {
     * @return boolean
     */
    protected function wantsTemporary($query) {
-       foreach($query->getWhere() as $whereClause) {
-           $from = array_values($query->getFrom());
-           // SQLQuery will automatically add double quotes and single quotes to values, so check against that.
-           if($whereClause == "{$from[0]}.\"MultiFormIsTemporary\" = '1'") {
-               return true;
+       foreach($query->getWhere() as $k => $v) {
+           // Compat for 3.1/3.2 where syntax
+           if(is_array($v)) {
+               // In >=3.2 each $condition is a single length array('condition' => array('params'))
+               $condition = $k;
+
+               // >=3.2 allows conditions to be expressed as evaluatable objects
+               if(interface_exists('SQLConditionGroup') && ($condition instanceof SQLConditionGroup)) {
+                   $condition = $condition->conditionSQL($params);
+               }
+
+               // SQLQuery will automatically add double quotes and single quotes to values, so check against that.
+               if(preg_match('/("|\'|`)MultiFormIsTemporary("|\'|`)/', $condition) && (int)$v[0] == 1) {
+                   return true;
+               }
+           } else {
+               $condition = $v;
+               // SQLQuery will automatically add double quotes and single quotes to values, so check against that.
+               if(preg_match('/("|\'|`)MultiFormIsTemporary("|\'|`) = \'1\'/', $condition)) {
+                   return true;
+               }
            }
        }

@chillu chillu changed the title from Update MultiFormObjectDecorator to be SS3 compatible to Update MultiFormObjectDecorator to be SS3.2 compatible Apr 30, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment