7 * Some doctrine helper functionality.
14 * Extract specified fields from a dataset.
15 * @param array $fields
19 static public function extractData(array $fields, array $data)
22 if (empty($data) || empty($fields))
27 #-> Find the data we have an interest in.
29 foreach ($data as $row)
33 if (!isset($mergeMap))
36 foreach ($row as $id => $nest)
42 foreach ($mergeMap as $id)
44 $row = array_merge($row, $row[$id]);
48 $result[] = self::extractDataFromRow($fields, $row);
56 * Extract specified fields from a [nested] data row.
57 * @param array $fields
61 static public function extractDataFromRow(array $fields, array $row)
64 foreach ($fields as $key => $field)
70 $result[$key] = isset($row[$key])
71 ? self::extractData($field, $row)
76 $result[$key] = isset($row[$key])
77 ? self::extractDataFromRow($field, $row[$key])
85 $result[$field] = isset($row[$field])
91 if (!is_object($field))
96 isset($dateTimeFormat)
97 || $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
98 $result[$key] = isset($row[$key]) && is_object($row[$key])
99 ? $row[$key]->format($dateTimeFormat)
103 isset($dateTimeFormat)
104 || $dateFormat = \Utility\Registry::getConfigParam('DateFormat');
105 $result[$key] = isset($row[$key]) && is_object($row[$key])
106 ? $row[$key]->format($dateFormat)
110 $result[$key] = isset($row[$key]) && !empty($row[$key])
111 ? unserialize($row[$key])
116 elseif (is_callable($field))
118 $result[$key] = $field(isset($row[$key]) ? $row[$key] : null);
127 * Build DQL order by string from array input.
128 * @param array $input
129 * @param boolean $final
132 static public function dqlOrder(array $input, $final = true)
135 foreach ($input as $field => $direction)
137 $direction = 'DESC' == strtoupper($direction)
140 $orderBy[] = "$field $direction";
142 return ($final && !empty($orderBy))
143 ? 'ORDER BY ' . implode(', ', $orderBy)
144 : implode(', ', $orderBy);
148 * Build DQL where statement from array input.
149 * @param array $input
150 * @param string $baseTable
151 * @param boolean $final
152 * @param number $prepend
155 static public function dqlFilter(array $input, $baseTable, $final = true, $prepend = 0, $op = ' AND ')
158 $subPrepend = $prepend;
162 foreach ($input as $field => $value)
165 $param = 'p' . $prepend . 'd' . $i;
167 && !strpos($field, '.')
168 && $field = "$baseTable.$field";
169 if (is_array($value))
172 $subOp = (' AND ' == $op)
176 $subFilter = self::dqlFilter($value, $baseTable, false, $subPrepend, $subOp);
177 $filter[] = '(' . $subFilter['Where'] . ')';
178 $params = array_merge($params, $subFilter['Params']);
180 elseif (false !== strpos($value, '%'))
182 $filter[] = "$field LIKE :$param";
183 $params[$param] = $value;
185 elseif ('BETWEEN' == substr(strtoupper($value), 0, 7))
188 $param2 = 'p' . $prepend . 'd' . $i;
189 list($x, $y) = explode(',', trim(substr($value, 7, strlen($value) - 2)));
190 $filter[] = "$field BETWEEN :$param AND :$param2";
191 $params[$param] = $x;
192 $params[$param2] = $y;
194 elseif ('NOT IN' == substr(strtoupper($value), 0, 6))
196 $filter[] = "$field NOT IN (:$param)";
197 $params[$param] = explode(',', trim(substr($value, 6, strlen($value) - 6)));
199 elseif ('=NULL' == strtoupper($value))
201 $filter[] = "$field IS NULL";
203 elseif ('!NULL' == strtoupper($value))
205 $filter[] = "$field IS NOT NULL";
207 elseif ('IN ' == substr(strtoupper($value), 0, 3))
209 $filter[] = "$field IN (:$param)";
210 $params[$param] = explode(',', trim(substr($value, 3, strlen($value) - 3)));
212 elseif ('!=' == substr($value, 0, 2))
214 $filter[] = "$field != :$param";
215 $params[$param] = trim(substr($value, 2, strlen($value) - 2));
217 elseif ('<>' == substr($value, 0, 2))
219 $filter[] = "$field <> :$param";
220 $params[$param] = trim(substr($value, 2, strlen($value) - 2));
222 elseif ('<=' == substr($value, 0, 2))
224 $filter[] = "$field <= :$param";
225 $params[$param] = trim(substr($value, 2, strlen($value) - 2));
227 elseif ('>=' == substr($value, 0, 2))
229 $filter[] = "$field >= :$param";
230 $params[$param] = trim(substr($value, 2, strlen($value) - 2));
232 elseif ('>' == substr($value, 0, 1))
234 $filter[] = "$field > :$param";
235 $params[$param] = trim(substr($value, 2, strlen($value) - 1));
237 elseif ('<' == substr($value, 0, 1))
239 $filter[] = "$field < :$param";
240 $params[$param] = trim(substr($value, 2, strlen($value) - 1));
242 elseif ('=' == substr($value, 0, 1))
244 $filter[] = "$field = :$param";
245 $params[$param] = trim(substr($value, 2, strlen($value) - 1));
247 elseif ('!' == substr($value, 0, 1))
249 $filter[] = "$field != :$param";
250 $params[$param] = trim(substr($value, 2, strlen($value) - 1));
254 $filter[] = "$field = :$param";
255 $params[$param] = $value;
259 'Where' => ($final && !empty($filter))
260 ? 'WHERE ' . implode($op, $filter)
261 : implode($op, $filter),
266 static public function lockTables(array $write = array(), array $read = array())
269 if (empty($write) && empty($read))
273 foreach ($write as $table)
275 $locks[] = "$table WRITE";
277 foreach ($read as $table)
279 $locks[] = "$table READ";
281 \Utility\Registry::getEntityManager()
283 ->exec('LOCK TABLES ' . implode(', ', $locks));
286 static public function unlockTables()
288 \Utility\Registry::getEntityManager()
290 ->exec('UNLOCK TABLES');
293 static public function setReadCommittedIsolation()
295 \Utility\Registry::getEntityManager()
297 ->exec("SET SESSION tx_isolation='READ-COMMITTED'");
300 static public function beginTransaction()
302 \Utility\Registry::getEntityManager()
304 ->exec('START TRANSACTION');
306 static public function commitTransaction()
308 \Utility\Registry::getEntityManager()
313 static public function rollbackTransaction()
315 \Utility\Registry::getEntityManager()