@@ -23,13 +23,15 @@ discard block |
||
| 23 | 23 | if (!isset($this->idCache[$id])) { |
| 24 | 24 | try { |
| 25 | 25 | $result = $this->selectQuery($this->selectBuilder->select($this->table, $pk . ' = :id', [':id' => $id], ['limit' => 1])); |
| 26 | - } |
|
| 27 | - catch (\Exception $e) { |
|
| 26 | + } catch (\Exception $e) { |
|
| 28 | 27 | // Don't issue an error if it cannot be found since we return null |
| 29 | 28 | } |
| 30 | 29 | |
| 31 | - if (isset($result[0])) $this->idCache[$id] = $result[0]; |
|
| 32 | - else return null; |
|
| 30 | + if (isset($result[0])) { |
|
| 31 | + $this->idCache[$id] = $result[0]; |
|
| 32 | + } else { |
|
| 33 | + return null; |
|
| 34 | + } |
|
| 33 | 35 | } |
| 34 | 36 | return $this->idCache[$id]; |
| 35 | 37 | } |
@@ -39,14 +41,15 @@ discard block |
||
| 39 | 41 | if (!isset($this->resultCache[$cacheId])) { |
| 40 | 42 | $query = $this->whereBuilder->createSql($fields); |
| 41 | 43 | |
| 42 | - if (!isset($options['order'])) $options['order'] = $defaultSort; |
|
| 44 | + if (!isset($options['order'])) { |
|
| 45 | + $options['order'] = $defaultSort; |
|
| 46 | + } |
|
| 43 | 47 | |
| 44 | 48 | try { |
| 45 | 49 | $this->resultCache[$cacheId] = $this->selectQuery($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
| 46 | 50 | $this->databaseModify->addIndex(array_keys($query['args'])); |
| 47 | 51 | $this->databaseModify->addIndex(explode(',', $options['order'])); |
| 48 | - } |
|
| 49 | - catch (\Exception $e) { |
|
| 52 | + } catch (\Exception $e) { |
|
| 50 | 53 | $this->errors[] = $e; |
| 51 | 54 | $this->resultCache[$cacheId] = []; |
| 52 | 55 | } |
@@ -56,7 +59,9 @@ discard block |
||
| 56 | 59 | |
| 57 | 60 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
| 58 | 61 | //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 |
| 59 | - if (is_array($field)) $field = $field[0]; |
|
| 62 | + if (is_array($field)) { |
|
| 63 | + $field = $field[0]; |
|
| 64 | + } |
|
| 60 | 65 | $query = $this->whereBuilder->createSql($criteria); |
| 61 | 66 | |
| 62 | 67 | try { |
@@ -65,8 +70,7 @@ discard block |
||
| 65 | 70 | $result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
| 66 | 71 | |
| 67 | 72 | return $this->determineAggregateResult($result, $group, $field); |
| 68 | - } |
|
| 69 | - catch (\Exception $e) { |
|
| 73 | + } catch (\Exception $e) { |
|
| 70 | 74 | return $group ? [] : 0; |
| 71 | 75 | } |
| 72 | 76 | } |
@@ -74,11 +78,15 @@ discard block |
||
| 74 | 78 | private function determineAggregateResult($result, $group, $field) { |
| 75 | 79 | if ($group != null) { |
| 76 | 80 | $ret = []; |
| 77 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 81 | + foreach ($result as $res) { |
|
| 82 | + $ret[$res->$field] = $res->val; |
|
| 83 | + } |
|
| 78 | 84 | return $ret; |
| 85 | + } else if (isset($result[0])) { |
|
| 86 | + return $result[0]->val; |
|
| 87 | + } else { |
|
| 88 | + return 0; |
|
| 79 | 89 | } |
| 80 | - else if (isset($result[0])) return $result[0]->val; |
|
| 81 | - else return 0; |
|
| 82 | 90 | } |
| 83 | 91 | |
| 84 | 92 | private function selectQuery(\Maphper\Lib\Query $query) { |
@@ -94,8 +102,11 @@ discard block |
||
| 94 | 102 | } |
| 95 | 103 | |
| 96 | 104 | public function updateCache($data, $pkValue) { |
| 97 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 98 | - else $this->cache[$pkValue] = $data; |
|
| 105 | + if (isset($this->cache[$pkValue])) { |
|
| 106 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 107 | + } else { |
|
| 108 | + $this->cache[$pkValue] = $data; |
|
| 109 | + } |
|
| 99 | 110 | } |
| 100 | 111 | |
| 101 | 112 | public function deleteIDFromCache($id) { |
@@ -88,10 +88,13 @@ discard block |
||
| 88 | 88 | $result = $this->insert($this->table, $this->primaryKey, $data); |
| 89 | 89 | |
| 90 | 90 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 91 | - if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
|
| 92 | - } |
|
| 93 | - catch (\Exception $e) { |
|
| 94 | - if (!$this->getTryAgain($tryagain)) throw $e; |
|
| 91 | + if ($result->errorCode() !== '00000') { |
|
| 92 | + throw new \Exception('Could not insert into ' . $this->table); |
|
| 93 | + } |
|
| 94 | + } catch (\Exception $e) { |
|
| 95 | + if (!$this->getTryAgain($tryagain)) { |
|
| 96 | + throw $e; |
|
| 97 | + } |
|
| 95 | 98 | |
| 96 | 99 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
| 97 | 100 | $this->save($data, false); |
@@ -108,21 +111,24 @@ discard block |
||
| 108 | 111 | } |
| 109 | 112 | |
| 110 | 113 | private function updatePK($data, $new) { |
| 111 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 114 | + if ($new && count($this->primaryKey) == 1) { |
|
| 115 | + $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 116 | + } |
|
| 112 | 117 | } |
| 113 | 118 | |
| 114 | 119 | private function checkIfUpdateWorked($data) { |
| 115 | 120 | $updateWhere = $this->whereBuilder->createSql($data); |
| 116 | 121 | $matched = $this->findByField($updateWhere['args']); |
| 117 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 122 | + if (count($matched) == 0) { |
|
| 123 | + throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 124 | + } |
|
| 118 | 125 | } |
| 119 | 126 | |
| 120 | 127 | private function insert($table, array $primaryKey, $data) { |
| 121 | 128 | $error = 0; |
| 122 | 129 | try { |
| 123 | 130 | $result = $this->adapter->query($this->crudBuilder->insert($table, $data)); |
| 124 | - } |
|
| 125 | - catch (\Exception $e) { |
|
| 131 | + } catch (\Exception $e) { |
|
| 126 | 132 | $error = 1; |
| 127 | 133 | } |
| 128 | 134 | |
@@ -135,7 +141,9 @@ discard block |
||
| 135 | 141 | |
| 136 | 142 | private function tryUpdate($table, array $primaryKey, $data) { |
| 137 | 143 | $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
| 138 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 144 | + if ($result->rowCount() === 0) { |
|
| 145 | + $this->checkIfUpdateWorked($data); |
|
| 146 | + } |
|
| 139 | 147 | |
| 140 | 148 | return $result; |
| 141 | 149 | } |