I think uploading packs is more or less working. I nutted out a few bugs in a lot...
[rock.divinelegy.git] / DataAccess / DataMapper / Helpers / VOMapsHelper.php
1 <?php
2
3 namespace DataAccess\DataMapper\Helpers;
4
5 use ReflectionClass;
6
7 class VOMapsHelper
8 {
9 private $_voName;
10 private $_tableName;
11 private $_accessor;
12
13 public function __construct($voName, $accessor, $tableName)
14 {
15 $this->_voName = $voName;
16
17 if($tableName) {
18 $this->_tableName = $tableName;
19 } else {
20 $this->_tableName = strtolower($voName);
21 }
22
23 if($accessor)
24 {
25 $this->_accessor = $accessor;
26 } else
27 {
28 $this->_accessor = 'get'. str_replace('_', '', $voName);
29 }
30 }
31
32 public function getAccessor()
33 {
34 return $this->_accessor;
35 }
36
37 public function getVOName()
38 {
39 return $this->_voName;
40 }
41
42 public function getTableName()
43 {
44 return $this->_tableName;
45 }
46
47 public function populate($maps, $db, $parent, $row)
48 {
49 $className = $maps[$this->_voName]['class'];
50 $table = $maps[$this->_voName]['table'];
51
52 // If the table we already have contains the id of a row we need in
53 // another table
54 //if(isset($row[$this->_tableName . '_id'])) {
55 if(array_key_exists($this->_tableName . '_id', $row)) { //this is a better choice as somtimes the array key is set, but is equal to null, and isset doesn't like that
56 $join_id = $row[$this->_tableName . '_id'];
57 $statement = $db->prepare(sprintf('SELECT * from %s WHERE id=%u',
58 $table,
59 $join_id));
60
61 $statement->execute();
62 $row = $statement->fetch();
63 } elseif($maps[$this->_voName]['table'] != $maps[$parent]['table']) {
64 // in this case we look in another table for this row's id
65 $join_id = $row['id'];
66 $statement = $db->prepare(sprintf('SELECT * from %s WHERE %s=%u',
67 $table,
68 strtolower($parent . '_id'),
69 $join_id
70 ));
71
72 $statement->execute();
73 $row = $statement->fetch();
74 }
75
76 $constructors = AbstractPopulationHelper::getConstrutorArray($maps, $this->_voName, $row, $db);
77
78 if(count($constructors) == 0)
79 {
80 return new $className;
81 } else {
82 $r = new ReflectionClass($className);
83 return $r->newInstanceArgs($constructors);
84 }
85 }
86 }