|
| 1 | +<?php |
| 2 | +namespace Automatorm\Orm; |
| 3 | + |
| 4 | +use HodgePodge\Common; |
| 5 | +use HodgePodge\Core; |
| 6 | +use HodgePodge\Core\Query; |
| 7 | +use Automatorm\Exception; |
| 8 | + |
| 9 | +class PartialResult |
| 10 | +{ |
| 11 | + protected $source; |
| 12 | + protected $sourceSchema; |
| 13 | + protected $currentSchema; |
| 14 | + protected $currentTable; |
| 15 | + protected $database; |
| 16 | + protected $route; |
| 17 | + protected $multiresult = false; |
| 18 | + |
| 19 | + public function __construct(Model $source) |
| 20 | + { |
| 21 | + $this->source = $source; |
| 22 | + $this->schema = $source->_data->getSchema(); |
| 23 | + $this->sourceSchema = $this->currentSchema = $source->_data->getModel(); |
| 24 | + $this->currentTable = $this->currentSchema['table']; |
| 25 | + $this->database = $source->_data->getDatabase(); |
| 26 | + } |
| 27 | + |
| 28 | + public function __get($var) |
| 29 | + { |
| 30 | + if (array_key_exists($var, $this->currentSchema['columns'])) |
| 31 | + { |
| 32 | + // We have column data, resolve! |
| 33 | + return $this->resolve($var); |
| 34 | + } |
| 35 | + |
| 36 | + if (array_key_exists($var, $this->currentSchema['one-to-many'])) |
| 37 | + { |
| 38 | + return $this->push12M($var, $this->currentSchema['one-to-many'][$var]); |
| 39 | + } |
| 40 | + |
| 41 | + if (array_key_exists($var, $this->currentSchema['many-to-one'])) |
| 42 | + { |
| 43 | + return $this->pushM21($var, $this->currentSchema['many-to-one'][$var]); |
| 44 | + } |
| 45 | + |
| 46 | + if (array_key_exists($var, $this->currentSchema['many-to-many'])) |
| 47 | + { |
| 48 | + return $this->pushM2M($var, $this->currentSchema['many-to-many'][$var]); |
| 49 | + } |
| 50 | + |
| 51 | + return $this->resolve($var); |
| 52 | + } |
| 53 | + |
| 54 | + public function resolve($var = null) |
| 55 | + { |
| 56 | + // Resolve down to a real Model object, then call __get on it. |
| 57 | + $ids = $this->resolveState(); |
| 58 | + |
| 59 | + $results = Model::factoryObjectCache($ids, $this->currentTable, $this->database); |
| 60 | + |
| 61 | + if ($this->multiresult && !$results instanceof Collection) |
| 62 | + { |
| 63 | + $results = new Collection([$results]); |
| 64 | + } |
| 65 | + |
| 66 | + if (!$this->multiresult && $results instanceof Collection && $results->count() == 1) |
| 67 | + { |
| 68 | + $results = $results[0]; |
| 69 | + } |
| 70 | + |
| 71 | + if (!is_null($var)) |
| 72 | + { |
| 73 | + return $results->{$var}; |
| 74 | + } |
| 75 | + return $results; |
| 76 | + } |
| 77 | + |
| 78 | + protected $joinCount = 0; |
| 79 | + |
| 80 | + public function resolveState() |
| 81 | + { |
| 82 | + $key = 'join_' . $this->joinCount; |
| 83 | + $select = |
| 84 | + 'Select distinct '.$key.'.id as id from `' . $this->sourceSchema['table_name'] . '` as join_0 '; |
| 85 | + |
| 86 | + $where = |
| 87 | + 'Where join_0.id = ' . $this->source->id; |
| 88 | + |
| 89 | + array_unshift($this->route, $select); |
| 90 | + array_push($this->route, $where); |
| 91 | + |
| 92 | + $query = new Query($this->database); |
| 93 | + $query->sql( |
| 94 | + implode(' ', $this->route) |
| 95 | + ); |
| 96 | + list($ids) = $query->execute(); |
| 97 | + |
| 98 | + foreach($ids as $id) |
| 99 | + { |
| 100 | + $return[] = $id['id']; |
| 101 | + } |
| 102 | + |
| 103 | + return $return; |
| 104 | + } |
| 105 | + |
| 106 | + public function push12M($col, $target) |
| 107 | + { |
| 108 | + $key = 'join_' . $this->joinCount; |
| 109 | + $key2 = 'join_' . ++$this->joinCount; |
| 110 | + |
| 111 | + $this->route[] = |
| 112 | + 'Join `' . Schema::underscoreCase($target['table']) . '` as ' . $key2 . ' on ' . $key . '.id = ' . $key2 . '.`' . $target['column_name'] . '` '; |
| 113 | + ; |
| 114 | + |
| 115 | + $this->next($target['table']); |
| 116 | + $this->multiresult = true; |
| 117 | + |
| 118 | + return $this; |
| 119 | + } |
| 120 | + |
| 121 | + public function pushM21($col, $target) |
| 122 | + { |
| 123 | + $key = 'join_' . $this->joinCount; |
| 124 | + $key2 = 'join_' . ++$this->joinCount; |
| 125 | + |
| 126 | + $this->route[] = |
| 127 | + 'Join `' . Schema::underscoreCase($target) . '` as ' . $key2 . ' on ' . $key2 . '.id = ' . $key . '.`' . $col . '_id` '; |
| 128 | + ; |
| 129 | + |
| 130 | + $this->next($target); |
| 131 | + |
| 132 | + return $this; |
| 133 | + } |
| 134 | + |
| 135 | + public function pushM2M($col, $target) |
| 136 | + { |
| 137 | + $key = 'join_' . $this->joinCount; |
| 138 | + $key2 = 'join_' . ++$this->joinCount; |
| 139 | + $key2a = $key2 . 'a'; |
| 140 | + |
| 141 | + var_dump($target); |
| 142 | + |
| 143 | + $this->route[] = |
| 144 | + 'Join `' . Schema::underscoreCase($target['pivot']) . '` as ' . $key2a . ' on ' . $key . '.id = ' . $key2a . '.`' . $target['id'] . '` '; |
| 145 | + ; |
| 146 | + $this->route[] = |
| 147 | + 'Join `' . Schema::underscoreCase($target['connections'][0]['table']) . '` as ' . $key2 . ' on ' . $key2 . '.id = ' . $key2a . '.`' . $target['connections'][0]['column'] . '` '; |
| 148 | + ; |
| 149 | + |
| 150 | + $this->next(Schema::underscoreCase($target['connections'][0]['table'])); |
| 151 | + $this->multiresult = true; |
| 152 | + |
| 153 | + return $this; |
| 154 | + } |
| 155 | + |
| 156 | + public function next($tablename) |
| 157 | + { |
| 158 | + $this->currentSchema = $this->schema->getTable($tablename); |
| 159 | + $this->currentTable = $tablename; |
| 160 | + } |
| 161 | +} |
0 commit comments