Allow updating with null objects.
authorCameron Ball <c.ball1729@gmail.com>
Sun, 4 Jan 2015 09:03:17 +0000 (17:03 +0800)
committerCameron Ball <c.ball1729@gmail.com>
Sun, 4 Jan 2015 09:03:17 +0000 (17:03 +0800)
DataAccess/DataMapper/DataMapper.php
DataAccess/DataMapper/Helpers/AbstractPopulationHelper.php
config/Routes.php

index a7bef4f..5c817dc 100644 (file)
@@ -70,7 +70,7 @@ class DataMapper implements IDataMapper
             $queries = AbstractPopulationHelper::generateUpdateSaveQuery($this->_maps, $entity, $entity->getId(), $this->_db);\r
             $mergeMap = array();\r
             $flattened = array();\r
-            \r
+\r
             foreach($queries as $index => $query)\r
             {\r
                 $this_table = $query['table'];\r
index fd14d52..9f2cae0 100644 (file)
@@ -39,9 +39,9 @@ class AbstractPopulationHelper
         return $constructors;\r
     }\r
     \r
-    static function generateUpdateSaveQuery($maps, $entity, $id, $db, &$queries = array(), $extraColumns = array())\r
+    static function generateUpdateSaveQuery($maps, $entity, $id, $db, &$queries = array(), $extraColumns = array(), $mapsIndex = null)\r
     {\r
-        $entityMapsIndex = self::getMapsNameFromEntityObject($entity, $maps);\r
+        $entityMapsIndex = isset($mapsIndex) ? $mapsIndex : self::getMapsNameFromEntityObject($entity, $maps);\r
 \r
         if($id)\r
         {\r
@@ -53,17 +53,29 @@ class AbstractPopulationHelper
         foreach($maps[$entityMapsIndex]['maps'] as $mapsHelper)\r
         {\r
             $accessor = $mapsHelper->getAccessor();\r
-            $property = $entity->{$accessor}();\r
+            $property = isset($entity) ? $entity->{$accessor}() : null;\r
 \r
             //sometimes children objects will be null, e.g., the banner for a simfile\r
-            //just skip them\r
-            if(!is_null($property)) \r
+            //just skip them.\r
+            //Tricky: only skip when we're making a new entity. In the case of\r
+            //existing ones we need to cater for null objects. For example a user\r
+            //might change their country to null.\r
+            if((!is_null($property) || (is_null($property) && $id))) \r
             {\r
                 switch(get_class($mapsHelper))\r
                 {\r
                     case 'DataAccess\DataMapper\Helpers\VOMapsHelper':\r
                         //we have a vo. Determine which way the reference is\r
-                        $voMapsIndex = self::getMapsNameFromEntityObject($property, $maps);\r
+                        //\r
+                        //TODO: This is how I used to do this, but it failed if the property was NULL.\r
+                        //I dunno if I will have to do a similar thing with entity references (next case block)\r
+                        //but I'm leaving this here as a reminder if I ever have to come back to thiat.\r
+                        //\r
+                        //Notice I also added mapsIndex to the generateUpdateSaveQuery thing, that's important.\r
+                        //When I call it in this case block you can see I added voMapsIndex as that argument.\r
+                        //God I hope I can remember this stuff in the future.\r
+                        //$voMapsIndex = self::getMapsNameFromEntityObject($property, $maps);\r
+                        $voMapsIndex = $mapsHelper->getVOName();\r
                         $refDir = self::getReferenceDirection(\r
                             $maps[$entityMapsIndex]['table'],\r
                             $maps[$voMapsIndex]['table'],\r
@@ -76,7 +88,7 @@ class AbstractPopulationHelper
                             // our table stores their ID, all we do is update\r
                             // our reference.\r
                             case self::REFERENCE_FORWARD:\r
-                                $voTableId = self::findVOInDB($maps, $property, $db);\r
+                                $voTableId = self::findVOInDB($maps, $voMapsIndex, $property, $db);\r
 \r
                                 if($id)\r
                                 {\r
@@ -119,15 +131,16 @@ class AbstractPopulationHelper
                                 break;\r
                             case self::REFERENCE_BACK:\r
                                 $voId = self::findVOInDB($maps,\r
+                                    $voMapsIndex,\r
                                     $property,\r
                                     $db,\r
                                     array(strtolower($entityMapsIndex . '_id') => $id));\r
                                 if($voId)\r
                                 {\r
-                                    self::generateUpdateSaveQuery($maps, $property, $voId, $db, $queries);   \r
+                                    self::generateUpdateSaveQuery($maps, $property, $voId, $db, $queries, null, $voMapsIndex);   \r
                                 } else {\r
                                     $extra = array(strtolower($entityMapsIndex . '_id') => '%MAIN_QUERY_ID%');\r
-                                    self::generateUpdateSaveQuery($maps, $property, NULL, $db, $queries, $extra);\r
+                                    self::generateUpdateSaveQuery($maps, $property, NULL, $db, $queries, $extra, $voMapsIndex);\r
                                 }\r
                                 break;\r
                         }\r
@@ -203,12 +216,18 @@ class AbstractPopulationHelper
                         break;\r
                     case 'DataAccess\DataMapper\Helpers\VarcharMapsHelper':\r
                         //XXX: pls magically fix all my character encoding issues.\r
-                        $property = mb_convert_encoding($property, "UTF-8", mb_detect_encoding($property, "UTF-8, ISO-8859-1, ISO-8859-15", true));\r
+                        $property = isset($property) ? mb_convert_encoding($property, "UTF-8", mb_detect_encoding($property, "UTF-8, ISO-8859-1, ISO-8859-15", true)) : NULL;\r
                         if($id){\r
                             //easy case, plain values in our table.\r
-                            $query .= sprintf('%s="%s", ',\r
-                                $mapsHelper->getColumnName(),\r
-                                $property);                        \r
+                            if(isset($property))\r
+                            {\r
+                                $query .= sprintf('%s="%s", ',\r
+                                    $mapsHelper->getColumnName(),\r
+                                    $property);     \r
+                            } else {\r
+                                $query .= sprintf('%s=NULL, ',\r
+                                    $mapsHelper->getColumnName());                                     \r
+                            }\r
                         } else {\r
                             $queryColumnNamesAndValues[$mapsHelper->getColumnName()] = $db->quote($property);\r
                         }\r
@@ -364,9 +383,9 @@ class AbstractPopulationHelper
     }\r
     \r
     // can use this when we reference a VO\r
-    static public function findVOInDB($maps, $VO, $db, $extraColumns = array())\r
+    static public function findVOInDB($maps, $mapsIndex, $VO, $db, $extraColumns = array())\r
     {\r
-        $mapsIndex = self::getMapsNameFromEntityObject($VO, $maps);\r
+        //$mapsIndex = self::getMapsNameFromEntityObject($VO, $maps);\r
         $table = $maps[$mapsIndex]['table'];\r
 \r
         //TODO: This may break everythign, but I _think_ if I pass extraColuns, it is always an id column.\r
index cb9e903..7e3afb7 100644 (file)
@@ -67,7 +67,6 @@ return [
     ],\r
     \r
     '/files/simfile/:hash' => [\r
-        'methods' => ['GET'],\r
         'controller' => 'File',\r
         'actions' => [\r
             'GET' => 'serveSimfileOrPack'\r