@@ -96,15 +96,15 @@ discard block |
||
| 96 | 96 | $value = $this->entity->wrap($this->relations, $value); |
| 97 | 97 | $this->dataSource->save($value); |
| 98 | 98 | $visibilityOverride->write($value); |
| 99 | - $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
| 99 | + $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | public function offsetExists($offset) { |
| 103 | 103 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
| 104 | - if (!empty($this->settings['filter'])) { |
|
| 105 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 106 | - return isset($data[0]); |
|
| 107 | - } |
|
| 104 | + if (!empty($this->settings['filter'])) { |
|
| 105 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 106 | + return isset($data[0]); |
|
| 107 | + } |
|
| 108 | 108 | return (bool) $this->dataSource->findById($offset); |
| 109 | 109 | } |
| 110 | 110 | |
@@ -114,10 +114,10 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | public function offsetGet($offset) { |
| 116 | 116 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
| 117 | - if (!empty($this->settings['filter'])) { |
|
| 118 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 119 | - return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
| 120 | - } |
|
| 117 | + if (!empty($this->settings['filter'])) { |
|
| 118 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 119 | + return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
| 120 | + } |
|
| 121 | 121 | return $this->entity->create($this->dataSource->findById($offset), $this->relations); |
| 122 | 122 | } |
| 123 | 123 | |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | else $limit = ''; |
| 115 | 115 | |
| 116 | 116 | $query = $this->selectBuilder->createSql($fields, $mode); |
| 117 | - $query['sql'] = array_filter($query['sql']); |
|
| 117 | + $query['sql'] = array_filter($query['sql']); |
|
| 118 | 118 | $this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $limit)); |
| 119 | 119 | $this->addIndex(array_keys($query['args'])); |
| 120 | 120 | |
@@ -123,23 +123,23 @@ discard block |
||
| 123 | 123 | $this->resultCache = []; |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - private function getIfNew($data) { |
|
| 127 | - $new = false; |
|
| 128 | - foreach ($this->primaryKey as $k) { |
|
| 129 | - if (empty($data->$k)) { |
|
| 130 | - $data->$k = null; |
|
| 131 | - $new = true; |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - return $new; |
|
| 135 | - } |
|
| 126 | + private function getIfNew($data) { |
|
| 127 | + $new = false; |
|
| 128 | + foreach ($this->primaryKey as $k) { |
|
| 129 | + if (empty($data->$k)) { |
|
| 130 | + $data->$k = null; |
|
| 131 | + $new = true; |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + return $new; |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | 137 | public function save($data, $tryagain = true) { |
| 138 | 138 | $tryagain = $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
| 139 | - $new = $this->getIfNew($data); |
|
| 139 | + $new = $this->getIfNew($data); |
|
| 140 | 140 | |
| 141 | 141 | try { |
| 142 | - $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 142 | + $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 143 | 143 | |
| 144 | 144 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 145 | 145 | if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
@@ -158,18 +158,18 @@ discard block |
||
| 158 | 158 | $this->updateCache($data); |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - private function checkIfUpdateWorked($data) { |
|
| 162 | - $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
|
| 163 | - $matched = $this->findByField($updateWhere->getArgs()); |
|
| 161 | + private function checkIfUpdateWorked($data) { |
|
| 162 | + $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
|
| 163 | + $matched = $this->findByField($updateWhere->getArgs()); |
|
| 164 | 164 | |
| 165 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 166 | - } |
|
| 165 | + if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - private function updateCache($data) { |
|
| 169 | - $pkValue = $data->{$this->primaryKey[0]}; |
|
| 168 | + private function updateCache($data) { |
|
| 169 | + $pkValue = $data->{$this->primaryKey[0]}; |
|
| 170 | 170 | if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
| 171 | 171 | else $this->cache[$pkValue] = $data; |
| 172 | - } |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | 174 | private function insert($table, array $primaryKey, $data) { |
| 175 | 175 | $error = 0; |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 187 | + if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 188 | 188 | |
| 189 | 189 | return $result; |
| 190 | 190 | } |
@@ -25,13 +25,13 @@ discard block |
||
| 25 | 25 | $this->crudBuilder = new \Maphper\Lib\CrudBuilder(); |
| 26 | 26 | $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
| 27 | 27 | |
| 28 | - $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array) $this->options->read('fields'))); |
|
| 28 | + $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array)$this->options->read('fields'))); |
|
| 29 | 29 | |
| 30 | - $this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey); |
|
| 30 | + $this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey); |
|
| 31 | 31 | |
| 32 | 32 | $this->alterDb = $this->options->getEditMode(); |
| 33 | 33 | |
| 34 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($table); |
|
| 34 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0, 500) == 1) $this->adapter->optimiseColumns($table); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | public function getPrimaryKey() { |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | |
| 168 | 168 | private function updateCache($data) { |
| 169 | 169 | $pkValue = $data->{$this->primaryKey[0]}; |
| 170 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 170 | + if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object)array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 171 | 171 | else $this->cache[$pkValue] = $data; |
| 172 | 172 | } |
| 173 | 173 | |
@@ -31,7 +31,9 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | $this->alterDb = $this->options->getEditMode(); |
| 33 | 33 | |
| 34 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($table); |
|
| 34 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) { |
|
| 35 | + $this->adapter->optimiseColumns($table); |
|
| 36 | + } |
|
| 35 | 37 | } |
| 36 | 38 | |
| 37 | 39 | public function getPrimaryKey() { |
@@ -47,20 +49,24 @@ discard block |
||
| 47 | 49 | if (!isset($this->cache[$id])) { |
| 48 | 50 | try { |
| 49 | 51 | $result = $this->adapter->query($this->selectBuilder->select($this->table, [$this->getPrimaryKey()[0] . ' = :id'], [':id' => $id], ['limit' => 1])); |
| 50 | - } |
|
| 51 | - catch (\Exception $e) { |
|
| 52 | + } catch (\Exception $e) { |
|
| 52 | 53 | $this->errors[] = $e; |
| 53 | 54 | } |
| 54 | 55 | |
| 55 | - if (isset($result[0])) $this->cache[$id] = $result[0]; |
|
| 56 | - else return null; |
|
| 56 | + if (isset($result[0])) { |
|
| 57 | + $this->cache[$id] = $result[0]; |
|
| 58 | + } else { |
|
| 59 | + return null; |
|
| 60 | + } |
|
| 57 | 61 | } |
| 58 | 62 | return $this->cache[$id]; |
| 59 | 63 | } |
| 60 | 64 | |
| 61 | 65 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
| 62 | 66 | //Cannot count/sum/max multiple fields, pick the first one. This should only come into play when trying to count() a mapper with multiple primary keys |
| 63 | - if (is_array($field)) $field = $field[0]; |
|
| 67 | + if (is_array($field)) { |
|
| 68 | + $field = $field[0]; |
|
| 69 | + } |
|
| 64 | 70 | $query = $this->selectBuilder->createSql($criteria, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
| 65 | 71 | |
| 66 | 72 | try { |
@@ -68,22 +74,27 @@ discard block |
||
| 68 | 74 | $this->addIndex(explode(',', $group)); |
| 69 | 75 | $result = $this->adapter->query($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
| 70 | 76 | |
| 71 | - if (isset($result[0]) && $group == null) return $result[0]->val; |
|
| 72 | - else if ($group != null) { |
|
| 77 | + if (isset($result[0]) && $group == null) { |
|
| 78 | + return $result[0]->val; |
|
| 79 | + } else if ($group != null) { |
|
| 73 | 80 | $ret = []; |
| 74 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 81 | + foreach ($result as $res) { |
|
| 82 | + $ret[$res->$field] = $res->val; |
|
| 83 | + } |
|
| 75 | 84 | return $ret; |
| 85 | + } else { |
|
| 86 | + return 0; |
|
| 76 | 87 | } |
| 77 | - else return 0; |
|
| 78 | - } |
|
| 79 | - catch (\Exception $e) { |
|
| 88 | + } catch (\Exception $e) { |
|
| 80 | 89 | $this->errors[] = $e; |
| 81 | 90 | return $group ? [] : 0; |
| 82 | 91 | } |
| 83 | 92 | } |
| 84 | 93 | |
| 85 | 94 | private function addIndex($args) { |
| 86 | - if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
|
| 95 | + if (self::EDIT_INDEX & $this->alterDb) { |
|
| 96 | + $this->adapter->addIndex($this->table, $args); |
|
| 97 | + } |
|
| 87 | 98 | } |
| 88 | 99 | |
| 89 | 100 | public function findByField(array $fields, $options = []) { |
@@ -91,7 +102,9 @@ discard block |
||
| 91 | 102 | if (!isset($this->resultCache[$cacheId])) { |
| 92 | 103 | $query = $this->selectBuilder->createSql($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
| 93 | 104 | |
| 94 | - if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
|
| 105 | + if (!isset($options['order'])) { |
|
| 106 | + $options['order'] = $this->defaultSort; |
|
| 107 | + } |
|
| 95 | 108 | |
| 96 | 109 | $query['sql'] = array_filter($query['sql']); |
| 97 | 110 | |
@@ -99,8 +112,7 @@ discard block |
||
| 99 | 112 | $this->resultCache[$cacheId] = $this->adapter->query($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
| 100 | 113 | $this->addIndex(array_keys($query['args'])); |
| 101 | 114 | $this->addIndex(explode(',', $options['order'])); |
| 102 | - } |
|
| 103 | - catch (\Exception $e) { |
|
| 115 | + } catch (\Exception $e) { |
|
| 104 | 116 | $this->errors[] = $e; |
| 105 | 117 | $this->resultCache[$cacheId] = []; |
| 106 | 118 | } |
@@ -109,9 +121,14 @@ discard block |
||
| 109 | 121 | } |
| 110 | 122 | |
| 111 | 123 | public function deleteByField(array $fields, array $options = [], $mode = null) { |
| 112 | - if ($mode == null) $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND; |
|
| 113 | - if (isset($options['limit']) != null) $limit = ' LIMIT ' . $options['limit']; |
|
| 114 | - else $limit = ''; |
|
| 124 | + if ($mode == null) { |
|
| 125 | + $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND; |
|
| 126 | + } |
|
| 127 | + if (isset($options['limit']) != null) { |
|
| 128 | + $limit = ' LIMIT ' . $options['limit']; |
|
| 129 | + } else { |
|
| 130 | + $limit = ''; |
|
| 131 | + } |
|
| 115 | 132 | |
| 116 | 133 | $query = $this->selectBuilder->createSql($fields, $mode); |
| 117 | 134 | $query['sql'] = array_filter($query['sql']); |
@@ -142,17 +159,21 @@ discard block |
||
| 142 | 159 | $result = $this->insert($this->table, $this->primaryKey, $data); |
| 143 | 160 | |
| 144 | 161 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 145 | - if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
|
| 146 | - } |
|
| 147 | - catch (\Exception $e) { |
|
| 162 | + if ($result->errorCode() !== '00000') { |
|
| 163 | + throw new \Exception('Could not insert into ' . $this->table); |
|
| 164 | + } |
|
| 165 | + } catch (\Exception $e) { |
|
| 148 | 166 | if ($tryagain) { |
| 149 | 167 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
| 150 | 168 | $this->save($data, false); |
| 169 | + } else { |
|
| 170 | + throw $e; |
|
| 151 | 171 | } |
| 152 | - else throw $e; |
|
| 153 | 172 | } |
| 154 | 173 | |
| 155 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 174 | + if ($new && count($this->primaryKey) == 1) { |
|
| 175 | + $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 176 | + } |
|
| 156 | 177 | //Something has changed, clear any cached results as they may now be incorrect |
| 157 | 178 | $this->resultCache = []; |
| 158 | 179 | $this->updateCache($data); |
@@ -162,21 +183,25 @@ discard block |
||
| 162 | 183 | $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
| 163 | 184 | $matched = $this->findByField($updateWhere->getArgs()); |
| 164 | 185 | |
| 165 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 186 | + if (count($matched) == 0) { |
|
| 187 | + throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 188 | + } |
|
| 166 | 189 | } |
| 167 | 190 | |
| 168 | 191 | private function updateCache($data) { |
| 169 | 192 | $pkValue = $data->{$this->primaryKey[0]}; |
| 170 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 171 | - else $this->cache[$pkValue] = $data; |
|
| 193 | + if (isset($this->cache[$pkValue])) { |
|
| 194 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 195 | + } else { |
|
| 196 | + $this->cache[$pkValue] = $data; |
|
| 197 | + } |
|
| 172 | 198 | } |
| 173 | 199 | |
| 174 | 200 | private function insert($table, array $primaryKey, $data) { |
| 175 | 201 | $error = 0; |
| 176 | 202 | try { |
| 177 | 203 | $result = $this->adapter->query($this->crudBuilder->insert($table, $data)); |
| 178 | - } |
|
| 179 | - catch (\Exception $e) { |
|
| 204 | + } catch (\Exception $e) { |
|
| 180 | 205 | $error = 1; |
| 181 | 206 | } |
| 182 | 207 | |
@@ -184,7 +209,9 @@ discard block |
||
| 184 | 209 | $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
| 185 | 210 | } |
| 186 | 211 | |
| 187 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 212 | + if ($result->rowCount() === 0) { |
|
| 213 | + $this->checkIfUpdateWorked($data); |
|
| 214 | + } |
|
| 188 | 215 | |
| 189 | 216 | return $result; |
| 190 | 217 | } |