More dataaccess stuff.
[rock.divinelegy.git] / DataAccess / DataMapper / DataMapper.php
1 <?php
2
3 namespace DataAccess\DataMapper;
4
5 use Domain\Entities\IDivineEntity;
6 use DataAccess\DataMapper\IDataMapper;
7 use DataAccess\DataMapper\Helpers\AbstractPopulationHelper;
8 use ReflectionClass;
9 use PDO;
10
11 class DataMapper implements IDataMapper
12 {
13 private $_db;
14 private $_maps;
15
16 public function __construct($maps)
17 {
18 //TODO: should probably do all this through a configuration object or something
19 $dsn = 'mysql:host=localhost;dbname=divinelegy;charset=utf8';
20 $username = 'root';
21 $password = 'toor';
22 $options = array(PDO::ATTR_EMULATE_PREPARES => false,
23 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
24
25 $this->_db = new PDO($dsn, $username, null, $options);
26 $this->_maps = include $maps;
27 }
28
29 public function map($entityName, $queryString = 'SELECT * FROM %s')
30 {
31 $statement = $this->_db->prepare(sprintf($queryString,
32 $this->_maps[$entityName]['table']
33 ));
34
35 $statement->execute();
36 $rows = $statement->fetchAll();
37
38 $entities = array();
39
40 foreach($rows as $row)
41 {
42 $className = $this->_maps[$entityName]['class']; //the entity to instantiate and return
43 $constructors = AbstractPopulationHelper::getConstrutorArray($this->_maps, $entityName, $row, $this->_db);
44
45 if(count($constructors) == 0)
46 {
47 $class = new $className;
48 } else {
49 $r = new ReflectionClass($className);
50 $class = $r->newInstanceArgs($constructors);
51 }
52
53 $class->setId($row['id']);
54 $entities[$row['id']] = $class;
55 }
56
57 return $entities;
58 }
59
60 public function save(IDivineEntity $entity)
61 {
62 $queries = AbstractPopulationHelper::generateUpdateSaveQuery($this->_maps, $entity, $entity->getId(), $this->_db);
63
64 // if($queries['TYPE'] == AbstractPopulationHelper::QUERY_TYPE_CREATE)
65 // {
66 unset($queries['TYPE']);
67 $idMap = [];
68 foreach($queries as $index => $query)
69 {
70 $runQuery = true;
71 if (preg_match_all('/'.preg_quote('%').'(.*?)'.preg_quote('%').'/s', $query, $matches)) {
72 foreach($matches[1] as $index_ref)
73 {
74 if($index_ref != 'MAIN_QUERY_ID')
75 {
76 $index_id = str_replace('INDEX_REF_', '', $index_ref);
77 $query = str_replace('%INDEX_REF_' . $index_id . '%', $idMap['INDEX_REF_' . $index_id], $query);
78 } else {
79 $runQuery = false;
80 }
81 }
82 }
83
84 if($runQuery)
85 {
86 $statement = $this->_db->prepare($query);
87 $statement->execute();
88 $refIndex = $index+1;
89 $idMap['INDEX_REF_' . $refIndex] = $this->_db->lastInsertId();
90 unset($queries[$index]);
91 } else {
92 //update query so that other references are resolved.
93 $queries[$index] = $query;
94 }
95 }
96
97 //at this point we have queries left that depend on the main query id
98 foreach($queries as $query)
99 {
100 $query = str_replace('%MAIN_QUERY_ID%', end($idMap), $query);
101 $statement = $this->_db->prepare($query);
102 $statement->execute();
103 }
104 //}
105
106 echo '<pre>';
107 print_r($queries);
108 echo '</pre>';
109 }
110
111 //TODO: Implement
112 public function remove(IDivineEntity $entity) {
113 ;
114 }
115 }