From 18eda7be76157a7e52addc3b7263df41d3898643 Mon Sep 17 00:00:00 2001 From: MGatner Date: Tue, 22 Oct 2019 10:49:57 -0400 Subject: [PATCH 1/2] Bugfix nesting data reference --- system/Model.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Model.php b/system/Model.php index 8749b2c17f97..368cb5506d59 100644 --- a/system/Model.php +++ b/system/Model.php @@ -719,7 +719,7 @@ public function insert($data = null, bool $returnID = true) } // Trigger afterInsert events with the inserted data and new ID - $this->trigger('afterInsert', ['id' => $this->insertID, 'data' => $data, 'result' => $result]); + $this->trigger('afterInsert', ['id' => $this->insertID, 'data' => $data['data'], 'result' => $result]); // If insertion failed, get out of here if (! $result) @@ -841,7 +841,7 @@ public function update($id = null, $data = null): bool ->set($data['data'], '', $escape) ->update(); - $this->trigger('afterUpdate', ['id' => $id, 'data' => $data, 'result' => $result]); + $this->trigger('afterUpdate', ['id' => $id, 'data' => $data['data'], 'result' => $result]); return $result; } From 406bdbe7ca0cc2bdaef0117012c6774a87f00a1d Mon Sep 17 00:00:00 2001 From: MGatner Date: Tue, 22 Oct 2019 10:59:54 -0400 Subject: [PATCH 2/2] Implement eventData for clear naming --- system/Model.php | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/system/Model.php b/system/Model.php index 368cb5506d59..86c1659ed4ca 100644 --- a/system/Model.php +++ b/system/Model.php @@ -381,12 +381,12 @@ public function find($id = null) $row = $row->getResult($this->tempReturnType); } - $row = $this->trigger('afterFind', ['id' => $id, 'data' => $row]); + $eventData = $this->trigger('afterFind', ['id' => $id, 'data' => $row]); $this->tempReturnType = $this->returnType; $this->tempUseSoftDeletes = $this->useSoftDeletes; - return $row['data']; + return $eventData['data']; } //-------------------------------------------------------------------- @@ -438,12 +438,12 @@ public function findAll(int $limit = 0, int $offset = 0) $row = $row->getResult($this->tempReturnType); - $row = $this->trigger('afterFind', ['data' => $row, 'limit' => $limit, 'offset' => $offset]); + $eventData = $this->trigger('afterFind', ['data' => $row, 'limit' => $limit, 'offset' => $offset]); $this->tempReturnType = $this->returnType; $this->tempUseSoftDeletes = $this->useSoftDeletes; - return $row['data']; + return $eventData['data']; } //-------------------------------------------------------------------- @@ -475,11 +475,11 @@ public function first() $row = $row->getFirstRow($this->tempReturnType); - $row = $this->trigger('afterFind', ['data' => $row]); + $eventData = $this->trigger('afterFind', ['data' => $row]); $this->tempReturnType = $this->returnType; - return $row['data']; + return $eventData['data']; } //-------------------------------------------------------------------- @@ -705,11 +705,11 @@ public function insert($data = null, bool $returnID = true) $data[$this->updatedField] = $date; } - $data = $this->trigger('beforeInsert', ['data' => $data]); + $eventData = $this->trigger('beforeInsert', ['data' => $data]); // Must use the set() method to ensure objects get converted to arrays $result = $this->builder() - ->set($data['data'], '', $escape) + ->set($eventData['data'], '', $escape) ->insert(); // If insertion succeeded then save the insert ID @@ -719,7 +719,7 @@ public function insert($data = null, bool $returnID = true) } // Trigger afterInsert events with the inserted data and new ID - $this->trigger('afterInsert', ['id' => $this->insertID, 'data' => $data['data'], 'result' => $result]); + $this->trigger('afterInsert', ['id' => $this->insertID, 'data' => $eventData['data'], 'result' => $result]); // If insertion failed, get out of here if (! $result) @@ -827,7 +827,7 @@ public function update($id = null, $data = null): bool $data[$this->updatedField] = $this->setDate(); } - $data = $this->trigger('beforeUpdate', ['id' => $id, 'data' => $data]); + $eventData = $this->trigger('beforeUpdate', ['id' => $id, 'data' => $data]); $builder = $this->builder(); @@ -838,10 +838,10 @@ public function update($id = null, $data = null): bool // Must use the set() method to ensure objects get converted to arrays $result = $builder - ->set($data['data'], '', $escape) + ->set($eventData['data'], '', $escape) ->update(); - $this->trigger('afterUpdate', ['id' => $id, 'data' => $data['data'], 'result' => $result]); + $this->trigger('afterUpdate', ['id' => $id, 'data' => $eventData['data'], 'result' => $result]); return $result; } @@ -1564,22 +1564,22 @@ public function countAllResults(bool $reset = true, bool $test = false) * It is the responsibility of the callback methods to return * the data itself. * - * Each $data array MUST have a 'data' key with the relevant + * Each $eventData array MUST have a 'data' key with the relevant * data for callback methods (like an array of key/value pairs to insert * or update, an array of results, etc) * * @param string $event - * @param array $data + * @param array $eventData * * @return mixed * @throws \CodeIgniter\Database\Exceptions\DataException */ - protected function trigger(string $event, array $data) + protected function trigger(string $event, array $eventData) { // Ensure it's a valid event if (! isset($this->{$event}) || empty($this->{$event})) { - return $data; + return $eventData; } foreach ($this->{$event} as $callback) @@ -1589,10 +1589,10 @@ protected function trigger(string $event, array $data) throw DataException::forInvalidMethodTriggered($callback); } - $data = $this->{$callback}($data); + $eventData = $this->{$callback}($eventData); } - return $data; + return $eventData; } //--------------------------------------------------------------------