@@ -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); |
@@ -157,22 +157,22 @@ discard block |
||
| 157 | 157 | $this->updateCache($data); |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | - private function updatePK($data, $new) { |
|
| 161 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 162 | - } |
|
| 160 | + private function updatePK($data, $new) { |
|
| 161 | + if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - private function checkIfUpdateWorked($data) { |
|
| 165 | - $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
|
| 166 | - $matched = $this->findByField($updateWhere->getArgs()); |
|
| 164 | + private function checkIfUpdateWorked($data) { |
|
| 165 | + $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
|
| 166 | + $matched = $this->findByField($updateWhere->getArgs()); |
|
| 167 | 167 | |
| 168 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 169 | - } |
|
| 168 | + if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - private function updateCache($data) { |
|
| 172 | - $pkValue = $data->{$this->primaryKey[0]}; |
|
| 171 | + private function updateCache($data) { |
|
| 172 | + $pkValue = $data->{$this->primaryKey[0]}; |
|
| 173 | 173 | if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
| 174 | 174 | else $this->cache[$pkValue] = $data; |
| 175 | - } |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | 177 | private function insert($table, array $primaryKey, $data) { |
| 178 | 178 | $error = 0; |
@@ -184,16 +184,16 @@ discard block |
||
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | if ($error || $result->errorCode() !== '00000') { |
| 187 | - $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 188 | - } |
|
| 187 | + $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | 190 | return $result; |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | - private function tryUpdate($table, array $primaryKey, $data) { |
|
| 194 | - $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 195 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 193 | + private function tryUpdate($table, array $primaryKey, $data) { |
|
| 194 | + $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 195 | + if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 196 | 196 | |
| 197 | - return $result; |
|
| 198 | - } |
|
| 197 | + return $result; |
|
| 198 | + } |
|
| 199 | 199 | } |
@@ -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,10 +159,13 @@ 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) { |
|
| 148 | - if (!$tryagain) throw $e; |
|
| 162 | + if ($result->errorCode() !== '00000') { |
|
| 163 | + throw new \Exception('Could not insert into ' . $this->table); |
|
| 164 | + } |
|
| 165 | + } catch (\Exception $e) { |
|
| 166 | + if (!$tryagain) { |
|
| 167 | + throw $e; |
|
| 168 | + } |
|
| 149 | 169 | |
| 150 | 170 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
| 151 | 171 | $this->save($data, false); |
@@ -158,28 +178,34 @@ discard block |
||
| 158 | 178 | } |
| 159 | 179 | |
| 160 | 180 | private function updatePK($data, $new) { |
| 161 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 181 | + if ($new && count($this->primaryKey) == 1) { |
|
| 182 | + $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 183 | + } |
|
| 162 | 184 | } |
| 163 | 185 | |
| 164 | 186 | private function checkIfUpdateWorked($data) { |
| 165 | 187 | $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
| 166 | 188 | $matched = $this->findByField($updateWhere->getArgs()); |
| 167 | 189 | |
| 168 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 190 | + if (count($matched) == 0) { |
|
| 191 | + throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 192 | + } |
|
| 169 | 193 | } |
| 170 | 194 | |
| 171 | 195 | private function updateCache($data) { |
| 172 | 196 | $pkValue = $data->{$this->primaryKey[0]}; |
| 173 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 174 | - else $this->cache[$pkValue] = $data; |
|
| 197 | + if (isset($this->cache[$pkValue])) { |
|
| 198 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 199 | + } else { |
|
| 200 | + $this->cache[$pkValue] = $data; |
|
| 201 | + } |
|
| 175 | 202 | } |
| 176 | 203 | |
| 177 | 204 | private function insert($table, array $primaryKey, $data) { |
| 178 | 205 | $error = 0; |
| 179 | 206 | try { |
| 180 | 207 | $result = $this->adapter->query($this->crudBuilder->insert($table, $data)); |
| 181 | - } |
|
| 182 | - catch (\Exception $e) { |
|
| 208 | + } catch (\Exception $e) { |
|
| 183 | 209 | $error = 1; |
| 184 | 210 | } |
| 185 | 211 | |
@@ -192,7 +218,9 @@ discard block |
||
| 192 | 218 | |
| 193 | 219 | private function tryUpdate($table, array $primaryKey, $data) { |
| 194 | 220 | $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
| 195 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 221 | + if ($result->rowCount() === 0) { |
|
| 222 | + $this->checkIfUpdateWorked($data); |
|
| 223 | + } |
|
| 196 | 224 | |
| 197 | 225 | return $result; |
| 198 | 226 | } |