$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
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
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
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
{\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
$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
{\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
\r
namespace Domain\Entities\StepMania;\r
\r
+use Domain\VOs\StepMania\IStepChart;\r
+\r
interface ISimfile\r
{\r
public function getTitle();\r
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
return $this->_bgChanges;\r
}\r
\r
- public function addStepChart(StepChart $stepChart) {\r
+ public function addStepChart(IStepChart $stepChart) {\r
$this->_steps[] = $stepChart;\r
}\r
\r
+++ /dev/null
-<?php
-namespace DivinElegy\BLL;
-
-class Artist
-{
-
-}
\ No newline at end of file
+++ /dev/null
-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.
+++ /dev/null
-<?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;
- }
-}
+++ /dev/null
-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.
+++ /dev/null
-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.
+++ /dev/null
-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.
-
-
+++ /dev/null
-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.
+++ /dev/null
-Abstract classes go here. EG:
-
-model
-view
-controller
-datamapper
-service
+++ /dev/null
-rock n roll
\r
//$user->setId(NULL);\r
\r
+$simfile = $DataMapper->find(1, 'Simfile');\r
+$simfile->addStepChart($stepChart);\r
$DataMapper->save($simfile);\r
\r
\r