From d35e1e20b136ca9708a783924f15404cd39edfe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Fri, 18 Aug 2017 19:34:55 +0100 Subject: [PATCH] Changed event behavior so it always fires with $queryObject available. --- .../QueryBuilder/QueryBuilderHandler.php | 48 ++++++++----------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php index c5d17b3..be8f6ca 100644 --- a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php +++ b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php @@ -190,11 +190,7 @@ public function statement($sql, $bindings = []) */ public function get() { - $eventResult = $this->fireEvents('before-select'); - if ($eventResult !== null) { - return $eventResult; - } - + $queryObject = null; $executionTime = 0; if ($this->pdoStatement === null) { $queryObject = $this->getQuery('select'); @@ -205,10 +201,11 @@ public function get() } $start = microtime(true); + $this->fireEvents('before-select', $queryObject); $result = call_user_func_array([$this->pdoStatement, 'fetchAll'], $this->fetchParameters); $executionTime += microtime(true) - $start; $this->pdoStatement = null; - $this->fireEvents('after-select', $result, $executionTime); + $this->fireEvents('after-select', $queryObject, $result, $executionTime); return $result; } @@ -347,36 +344,37 @@ public function subQuery(QueryBuilderHandler $queryBuilder, $alias = null) */ private function doInsert($data, $type) { - $eventResult = $this->fireEvents('before-insert'); - if ($eventResult !== null) { - return $eventResult; - } + $queryObject = null; // If first value is not an array // Its not a batch insert if (!is_array(current($data))) { $queryObject = $this->getQuery($type, $data); + $this->fireEvents('before-insert', $queryObject); list($result, $executionTime) = $this->statement($queryObject->getSql(), $queryObject->getBindings()); - $return = $result->rowCount() === 1 ? $this->pdo->lastInsertId() : null; + $this->fireEvents('after-insert', $queryObject, $return, $executionTime); + } else { // Its a batch insert $return = []; - $executionTime = 0; foreach ($data as $subData) { $queryObject = $this->getQuery($type, $subData); - list($result, $time) = $this->statement($queryObject->getSql(), $queryObject->getBindings()); - $executionTime += $time; + $this->fireEvents('before-insert', $queryObject); + + list($result, $executionTime) = $this->statement($queryObject->getSql(), $queryObject->getBindings()); + + $result = $result->rowCount() === 1 ? $this->pdo->lastInsertId() : null; + + $this->fireEvents('after-insert', $queryObject, $result, $executionTime); - if ($result->rowCount() === 1) { - $return[] = $this->pdo->lastInsertId(); - } + $return[] = $result; } } - $this->fireEvents('after-insert', $return, $executionTime); + return $return; } @@ -418,13 +416,10 @@ public function replace($data) */ public function update($data) { - $eventResult = $this->fireEvents('before-update'); - if ($eventResult !== null) { - return $eventResult; - } - $queryObject = $this->getQuery('update', $data); + $this->fireEvents('before-update', $queryObject); + list($response, $executionTime) = $this->statement($queryObject->getSql(), $queryObject->getBindings()); $this->fireEvents('after-update', $queryObject, $executionTime); @@ -461,13 +456,10 @@ public function onDuplicateKeyUpdate($data) */ public function delete() { - $eventResult = $this->fireEvents('before-delete'); - if ($eventResult !== null) { - return $eventResult; - } - $queryObject = $this->getQuery('delete'); + $this->fireEvents('before-delete', $queryObject); + list($response, $executionTime) = $this->statement($queryObject->getSql(), $queryObject->getBindings()); $this->fireEvents('after-delete', $queryObject, $executionTime);