@@ -25,16 +25,21 @@ discard block |
||
| 25 | 25 | if (($this->cacheMode && !isset($this->idCache[$id])) || !$this->cacheMode) { |
| 26 | 26 | try { |
| 27 | 27 | $result = $this->selectQuery($this->selectBuilder->select($this->table, $pk . ' = :id', [':id' => $id], ['limit' => 1])); |
| 28 | - } |
|
| 29 | - catch (\Exception $e) { |
|
| 28 | + } catch (\Exception $e) { |
|
| 30 | 29 | // Don't issue an error if it cannot be found since we return null |
| 31 | 30 | } |
| 32 | 31 | |
| 33 | - if (isset($result[0])) $result = $result[0]; |
|
| 34 | - else return null; |
|
| 32 | + if (isset($result[0])) { |
|
| 33 | + $result = $result[0]; |
|
| 34 | + } else { |
|
| 35 | + return null; |
|
| 36 | + } |
|
| 35 | 37 | |
| 36 | - if (!$this->cacheMode) return $result; |
|
| 37 | - else return $this->idCache[$id] = $result; |
|
| 38 | + if (!$this->cacheMode) { |
|
| 39 | + return $result; |
|
| 40 | + } else { |
|
| 41 | + return $this->idCache[$id] = $result; |
|
| 42 | + } |
|
| 38 | 43 | } |
| 39 | 44 | // cacheMode is true and cache is set |
| 40 | 45 | return $this->idCache[$id]; |
@@ -46,22 +51,27 @@ discard block |
||
| 46 | 51 | if (($this->cacheMode && !isset($this->resultCache[$cacheId])) || !$this->cacheMode) { |
| 47 | 52 | $query = $this->whereBuilder->createSql($fields); |
| 48 | 53 | |
| 49 | - if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
|
| 54 | + if (!isset($options['order'])) { |
|
| 55 | + $options['order'] = $this->defaultSort; |
|
| 56 | + } |
|
| 50 | 57 | |
| 51 | 58 | try { |
| 52 | 59 | $result = $this->selectQuery($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
| 53 | 60 | $this->databaseModify->addIndex(array_keys($query['args'])); |
| 54 | 61 | $this->databaseModify->addIndex(explode(',', $options['order'])); |
| 55 | - } |
|
| 56 | - catch (\Exception $e) { |
|
| 62 | + } catch (\Exception $e) { |
|
| 57 | 63 | $this->errors[] = $e; |
| 58 | 64 | $result = []; |
| 59 | 65 | } |
| 60 | 66 | } |
| 61 | 67 | |
| 62 | 68 | if ($this->cacheMode) { |
| 63 | - if (isset($result)) $this->resultCache[$cacheId] = $result; |
|
| 64 | - if (isset($this->resultCache[$cacheId])) return $this->resultCache[$cacheId]; |
|
| 69 | + if (isset($result)) { |
|
| 70 | + $this->resultCache[$cacheId] = $result; |
|
| 71 | + } |
|
| 72 | + if (isset($this->resultCache[$cacheId])) { |
|
| 73 | + return $this->resultCache[$cacheId]; |
|
| 74 | + } |
|
| 65 | 75 | } |
| 66 | 76 | |
| 67 | 77 | return $result; |
@@ -69,7 +79,9 @@ discard block |
||
| 69 | 79 | |
| 70 | 80 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
| 71 | 81 | //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 |
| 72 | - if (is_array($field)) $field = $field[0]; |
|
| 82 | + if (is_array($field)) { |
|
| 83 | + $field = $field[0]; |
|
| 84 | + } |
|
| 73 | 85 | $query = $this->whereBuilder->createSql($criteria); |
| 74 | 86 | |
| 75 | 87 | try { |
@@ -78,8 +90,7 @@ discard block |
||
| 78 | 90 | $result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
| 79 | 91 | |
| 80 | 92 | return $this->determineAggregateResult($result, $group, $field); |
| 81 | - } |
|
| 82 | - catch (\Exception $e) { |
|
| 93 | + } catch (\Exception $e) { |
|
| 83 | 94 | return $group ? [] : 0; |
| 84 | 95 | } |
| 85 | 96 | } |
@@ -87,11 +98,15 @@ discard block |
||
| 87 | 98 | private function determineAggregateResult($result, $group, $field) { |
| 88 | 99 | if ($group != null) { |
| 89 | 100 | $ret = []; |
| 90 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 101 | + foreach ($result as $res) { |
|
| 102 | + $ret[$res->$field] = $res->val; |
|
| 103 | + } |
|
| 91 | 104 | return $ret; |
| 105 | + } else if (isset($result[0])) { |
|
| 106 | + return $result[0]->val; |
|
| 107 | + } else { |
|
| 108 | + return 0; |
|
| 92 | 109 | } |
| 93 | - else if (isset($result[0])) return $result[0]->val; |
|
| 94 | - else return 0; |
|
| 95 | 110 | } |
| 96 | 111 | |
| 97 | 112 | private function selectQuery(\Maphper\Lib\Query $query) { |
@@ -99,21 +114,30 @@ discard block |
||
| 99 | 114 | } |
| 100 | 115 | |
| 101 | 116 | public function clearResultCache() { |
| 102 | - if ($this->cacheMode) $this->resultCache = []; |
|
| 117 | + if ($this->cacheMode) { |
|
| 118 | + $this->resultCache = []; |
|
| 119 | + } |
|
| 103 | 120 | } |
| 104 | 121 | |
| 105 | 122 | public function clearIDCache() { |
| 106 | - if ($this->cacheMode) $this->idCache = []; |
|
| 123 | + if ($this->cacheMode) { |
|
| 124 | + $this->idCache = []; |
|
| 125 | + } |
|
| 107 | 126 | } |
| 108 | 127 | |
| 109 | 128 | public function updateCache($data, $pkValue) { |
| 110 | 129 | if ($this->cacheMode) { |
| 111 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 112 | - else $this->cache[$pkValue] = $data; |
|
| 130 | + if (isset($this->cache[$pkValue])) { |
|
| 131 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 132 | + } else { |
|
| 133 | + $this->cache[$pkValue] = $data; |
|
| 134 | + } |
|
| 113 | 135 | } |
| 114 | 136 | } |
| 115 | 137 | |
| 116 | 138 | public function deleteIDFromCache($id) { |
| 117 | - if ($this->cacheMode) unset($this->idCache[$id]); |
|
| 139 | + if ($this->cacheMode) { |
|
| 140 | + unset($this->idCache[$id]); |
|
| 141 | + } |
|
| 118 | 142 | } |
| 119 | 143 | } |