Data mapper now resolves and runs SQL queries. Cleanup some entities.
authorCameron Ball <cameron@getapproved.com.au>
Fri, 5 Sep 2014 07:07:08 +0000 (15:07 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Fri, 5 Sep 2014 07:07:08 +0000 (15:07 +0800)
14 files changed:
DataAccess/DataMapper/DataMapper.php
DataAccess/DataMapper/Helpers/AbstractPopulationHelper.php
Domain/Entities/StepMania/ISimfile.php
Domain/Entities/StepMania/Simfile.php
app/bll/Artists.php [deleted file]
app/bll/README.md [deleted file]
app/bll/Simfile.php [deleted file]
app/datamappers/README.md [deleted file]
app/model/README.md [deleted file]
app/services/README.md [deleted file]
app/view/README.md [deleted file]
lib/base/README.md [deleted file]
public_html/index.html [deleted file]
public_html/index.php

index 07b4011..7026d16 100644 (file)
@@ -22,7 +22,7 @@ class DataMapper implements IDataMapper
         $options = array(PDO::ATTR_EMULATE_PREPARES => false,\r
                          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);\r
         \r
-        $this->_db = new PDO($dsn, $username, $password, $options);        \r
+        $this->_db = new PDO($dsn, $username, null, $options);        \r
         $this->_maps = include $maps;\r
     }\r
     \r
@@ -52,6 +52,49 @@ class DataMapper implements IDataMapper
     public function save(IDivineEntity $entity)\r
     {\r
         $queries = AbstractPopulationHelper::generateUpdateSaveQuery($this->_maps, $entity, $entity->getId(), $this->_db);\r
+        \r
+       // if($queries['TYPE'] == AbstractPopulationHelper::QUERY_TYPE_CREATE)\r
+       // {\r
+            unset($queries['TYPE']);\r
+            $idMap = [];\r
+            foreach($queries as $index => $query)\r
+            {                                \r
+                $runQuery = true;\r
+                if (preg_match_all('/'.preg_quote('%').'(.*?)'.preg_quote('%').'/s', $query, $matches)) {\r
+                    foreach($matches[1] as $index_ref)\r
+                    {\r
+                        if($index_ref != 'MAIN_QUERY_ID')\r
+                        {\r
+                            $index_id = str_replace('INDEX_REF_', '', $index_ref);\r
+                            $query = str_replace('%INDEX_REF_' . $index_id . '%', $idMap['INDEX_REF_' . $index_id], $query);\r
+                        } else {\r
+                            $runQuery = false;\r
+                        }\r
+                    }\r
+                }\r
+\r
+                if($runQuery)\r
+                {\r
+                    $statement = $this->_db->prepare($query);\r
+                    $statement->execute();\r
+                    $refIndex = $index+1;\r
+                    $idMap['INDEX_REF_' . $refIndex] = $this->_db->lastInsertId();\r
+                    unset($queries[$index]);\r
+                } else {\r
+                    //update query so that other references are resolved.\r
+                    $queries[$index] = $query;\r
+                }\r
+            }\r
+            \r
+            //at this point we have queries left that depend on the main query id\r
+            foreach($queries as $query)\r
+            {\r
+                $query = str_replace('%MAIN_QUERY_ID%', end($idMap), $query);\r
+                $statement = $this->_db->prepare($query);\r
+                $statement->execute();\r
+            }\r
+        //}\r
+        \r
         echo '<pre>';\r
         print_r($queries);\r
         echo '</pre>';\r
index 6ea6490..6f17458 100644 (file)
@@ -12,6 +12,8 @@ class AbstractPopulationHelper
     const REFERENCE_FORWARD = 1;\r
     const REFERENCE_BACK = 2;\r
     const REFERENCE_SELF = 3;\r
+    const QUERY_TYPE_UPDATE = 'update';\r
+    const QUERY_TYPE_CREATE = 'create';\r
     \r
     static function getConstrutorArray($maps, $entity, $row, $db)\r
     {\r
@@ -74,7 +76,7 @@ class AbstractPopulationHelper
                             if($id)\r
                             {\r
                                 $query .= sprintf('%s=%u, ',\r
-                                    strtolower($mapsHelper->getVOName() . '_id'),\r
+                                    strtolower($mapsHelper->getTableName() . '_id'),\r
                                     $voTableId);\r
                             } else {\r
                                 // we have a forward reference to a value object.\r
@@ -98,13 +100,14 @@ class AbstractPopulationHelper
                             {\r
                                 if($id)\r
                                 {\r
-                                    //TODO: logic to determine what the value is? i.e., string, int etc?\r
-                                    $query .= sprintf('%s="%s, "',\r
+                                    //TODO: logic to detemine what the value is? i.e., string, int etc?\r
+                                    $query .= sprintf('%s="%s", ',\r
                                         $columnName,\r
                                         $columnValue\r
                                     );\r
                                 } else {\r
-                                    $queryColumnNamesAndValues[$columnName] = $columnValue;\r
+                                    //TODO: logic to detemine what the value is? i.e., string, int etc?\r
+                                    $queryColumnNamesAndValues[$columnName] = sprintf('"%s"', $columnValue);\r
                                 }\r
                             }\r
 \r
@@ -226,10 +229,16 @@ class AbstractPopulationHelper
                         $voIds = self::mapVOArrayToIds($maps[$subEntityMapsIndex]['table'],\r
                             array(strtolower($entityMapsIndex . '_id'), $id),\r
                             $db);\r
-\r
+                        \r
                         foreach($property as $index => $propertyArrayElement)\r
                         {\r
-                            self::generateUpdateSaveQuery($maps, $propertyArrayElement, $voIds[$index], $db, $queries);\r
+                            $extra = array(strtolower($entityMapsIndex . '_id') => $id);\r
+                            if(isset($voIds[$index]))\r
+                            {\r
+                                self::generateUpdateSaveQuery($maps, $propertyArrayElement, $voIds[$index], $db, $queries, $extra);\r
+                            } else {\r
+                                self::generateUpdateSaveQuery($maps, $propertyArrayElement, NULL, $db, $queries, $extra);\r
+                            }\r
                         }\r
 \r
                         break;\r
@@ -250,12 +259,14 @@ class AbstractPopulationHelper
         {\r
             $query = substr($query, 0, -2);\r
             $query .= sprintf(' WHERE id=%u', $id);\r
+            $queries['TYPE'] = self::QUERY_TYPE_UPDATE;\r
         } else {\r
             $queryColumnNamesAndValues = array_merge($queryColumnNamesAndValues, $extraColumns);\r
             $query = sprintf('INSERT INTO %s (%s) VALUES (%s)',\r
                 $maps[$entityMapsIndex]['table'],\r
                 implode(', ', array_keys($queryColumnNamesAndValues)),\r
                 implode(', ', $queryColumnNamesAndValues));\r
+            $queries['TYPE'] = self::QUERY_TYPE_CREATE;\r
         }\r
         \r
         $queries[] = $query;\r
index 8957eb4..e25aac8 100644 (file)
@@ -2,6 +2,8 @@
 \r
 namespace Domain\Entities\StepMania;\r
 \r
+use Domain\VOs\StepMania\IStepChart;\r
+\r
 interface ISimfile\r
 {\r
     public function getTitle();\r
@@ -13,6 +15,6 @@ interface ISimfile
     public function hasFgChanges();\r
     public function hasBgChanges();\r
         \r
-    public function addStepChart(StepChart $stepChart);\r
+    public function addStepChart(IStepChart $stepChart);\r
     public function getSteps();\r
 }
\ No newline at end of file
index ff1da4e..e1941a5 100644 (file)
@@ -92,7 +92,7 @@ class Simfile extends AbstractEntity implements ISimfile
         return $this->_bgChanges;\r
     }\r
     \r
-    public function addStepChart(StepChart $stepChart) {\r
+    public function addStepChart(IStepChart $stepChart) {\r
         $this->_steps[] = $stepChart;\r
     }\r
     \r
diff --git a/app/bll/Artists.php b/app/bll/Artists.php
deleted file mode 100644 (file)
index 008951d..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-<?php
-namespace DivinElegy\BLL;
-
-class Artist
-{
-    
-}
\ No newline at end of file
diff --git a/app/bll/README.md b/app/bll/README.md
deleted file mode 100644 (file)
index 8fec559..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-Business objects go here. The should be completely decoupled from anything else. An example of a business object could be a simfile class.
-
-Whether or not the should implement an interface or extend an abstract class I have not yet decided. For now I will just implement the classes.
diff --git a/app/bll/Simfile.php b/app/bll/Simfile.php
deleted file mode 100644 (file)
index b6eee58..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-<?php
-namespace DivinElegy\BLL;
-
-class Simfile
-{
-    //str - simfile title
-    protected $title;
-    
-    //obj - reerence to song artist
-    protected $artist;
-    
-    //obj - reference to uploader
-    protected $uploader;
-    
-    //obj - reference to bpm object
-    protected $bpm;
-    
-    //bool - does the chart have stops
-    protected $stops;
-    
-    //bool - does the chart have fgChanges
-    protected $fgChanges;
-    
-    //bool - does the charg have bgChanges
-    protected $bgChanges;
-    
-    public function __construct($title)
-    {
-        $this->setTitle($title);
-    }
-    
-    protected function setTitle($title) 
-    {
-        if (empty($title))
-        {
-            throw new \InvalidArgumentException('Simfile title cannot be empty');
-        }
-        
-        $this->title = $title;
-    }
-    
-    public function setArtist(Artist $artist)
-    {
-        $this->artist = $artist;
-    }
-}
diff --git a/app/datamappers/README.md b/app/datamappers/README.md
deleted file mode 100644 (file)
index 74ec68e..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-This is where datamappers go, they need to extend the datamapper abstract class.
-
-The only thing a datamapper needs to know is how to save objects to the database. So SQL should go in a datamapper.
-
-I want to use PDO, so a datamapper will need a PDO instance.
diff --git a/app/model/README.md b/app/model/README.md
deleted file mode 100644 (file)
index 941fa36..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-This is the model LAYER. In here there should probably be three factory classes:
-
-BusinesObjectFactory - this knows how to build a business object.
-DataMapperFactory - this knows how to build a datamapper. DataMappers populate/save business objects by communicating with the database
-ServiceFactory - to build services. Services facilitate communication between business objects and datamappers.
-
-
-Since the model is a LAYER in this application there is no one model class, instead we pass the same instance of the ServiceFactory to views
-and controllers. The services contain the DataMapperFactory and BusinessObjectFactory.
diff --git a/app/services/README.md b/app/services/README.md
deleted file mode 100644 (file)
index 8132d96..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-Services go here.
-
-A service extends the service abstract class. Services interact with the model layer, Services need a BusinessObjectFactory and a DataMapperFactory. A simfile service would know
-how to build a simfile class.
-
-Saving state would be done in a service class.
-
-
diff --git a/app/view/README.md b/app/view/README.md
deleted file mode 100644 (file)
index 1e46684..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-This is where a view goes. Views need to extend the view abstract class.
-
-A view should only need the servicefactory as the a service facilitates the interaction with the model layer.
diff --git a/lib/base/README.md b/lib/base/README.md
deleted file mode 100644 (file)
index 9b1c26b..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-Abstract classes go here. EG:
-
-model
-view
-controller
-datamapper
-service
diff --git a/public_html/index.html b/public_html/index.html
deleted file mode 100644 (file)
index b52e774..0000000
+++ /dev/null
@@ -1 +0,0 @@
-rock n roll
index bef36f7..b40bf61 100644 (file)
@@ -57,6 +57,8 @@ $simfile = $simfileStepByStepBuilder->With_Title('Brand New Simfile')
 \r
 //$user->setId(NULL);\r
 \r
+$simfile = $DataMapper->find(1, 'Simfile');\r
+$simfile->addStepChart($stepChart);\r
 $DataMapper->save($simfile);\r
 \r
 \r