From 210a3ab3144939d5574b239dbbe1a47625c870dc Mon Sep 17 00:00:00 2001 From: Najdanovic Ivan Date: Wed, 6 Jan 2021 21:04:44 +0100 Subject: [PATCH] BaseModel/Model - Reworked escape parameter --- system/BaseModel.php | 19 ++++++++----------- system/Model.php | 36 +++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/system/BaseModel.php b/system/BaseModel.php index 5364d8dc6ad1..daf075ba8450 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -354,12 +354,11 @@ abstract protected function doFirst(); * Inserts data into the current database * This methods works only with dbCalls * - * @param array $data Data - * @param boolean|null $escape Escape + * @param array $data Data * * @return object|integer|string|false */ - abstract protected function doInsert(array $data, ?bool $escape = null); + abstract protected function doInsert(array $data); /** * Compiles batch insert and runs the queries, validating each row prior. @@ -708,13 +707,12 @@ public function getInsertID() * * @param array|object|null $data Data * @param boolean $returnID Whether insert ID should be returned or not. - * @param boolean|null $escape Escape * * @return BaseResult|object|integer|string|false * * @throws ReflectionException */ - public function insert($data = null, bool $returnID = true, ?bool $escape = null) + public function insert($data = null, bool $returnID = true) { $this->insertID = 0; @@ -774,7 +772,7 @@ public function insert($data = null, bool $returnID = true, ?bool $escape = null $eventData = $this->trigger('beforeInsert', $eventData); } - $result = $this->doInsert($eventData['data'], $escape); + $result = $this->doInsert($eventData['data']); $eventData = [ 'id' => $this->insertID, @@ -866,15 +864,14 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch * Updates a single record in the database. If an object is provided, * it will attempt to convert it into an array. * - * @param integer|array|string|null $id ID - * @param array|object|null $data Data - * @param boolean|null $escape Escape + * @param integer|array|string|null $id ID + * @param array|object|null $data Data * * @return boolean * * @throws ReflectionException */ - public function update($id = null, $data = null, ?bool $escape = null): bool + public function update($id = null, $data = null): bool { if (is_numeric($id) || is_string($id)) { @@ -936,7 +933,7 @@ public function update($id = null, $data = null, ?bool $escape = null): bool $eventData = [ 'id' => $id, 'data' => $eventData['data'], - 'result' => $this->doUpdate($id, $eventData['data'], $escape), + 'result' => $this->doUpdate($id, $eventData['data']), ]; if ($this->tempAllowCallbacks) diff --git a/system/Model.php b/system/Model.php index ef3bc0f010f5..bc3a181f7314 100644 --- a/system/Model.php +++ b/system/Model.php @@ -84,6 +84,13 @@ class Model extends BaseModel */ protected $tempData = []; + /** + * Escape Parameter to be passed in do methods + * + * @var boolean|null + */ + protected $escape = null; + // endregion // region Constructor @@ -243,13 +250,15 @@ protected function doFirst() * Inserts data into the current table. * This methods works only with dbCalls * - * @param array $data Data - * @param boolean|null $escape Escape + * @param array $data Data * * @return BaseResult|integer|string|false */ - protected function doInsert(array $data, ?bool $escape = null) + protected function doInsert(array $data) { + $escape = $this->escape; + $this->escape = null; + // Require non empty primaryKey when // not using auto-increment feature if (! $this->useAutoIncrement && empty($data[$this->primaryKey])) @@ -320,6 +329,9 @@ protected function doInsertBatch(?array $set = null, ?bool $escape = null, int $ */ protected function doUpdate($id = null, $data = null, ?bool $escape = null): bool { + $escape = $this->escape; + $this->escape = null; + $builder = $this->builder(); if ($id) @@ -654,46 +666,44 @@ protected function shouldUpdate($data) : bool * * @param array|object|null $data Data * @param boolean $returnID Whether insert ID should be returned or not. - * @param boolean|null $escape Escape * * @return BaseResult|object|integer|string|false * * @throws ReflectionException */ - public function insert($data = null, bool $returnID = true, ?bool $escape = null) + public function insert($data = null, bool $returnID = true) { if (empty($data)) { $data = $this->tempData['data'] ?? null; - $escape = $this->tempData['escape'] ?? null; + $this->escape = $this->tempData['escape'] ?? null; $this->tempData = []; } - return parent::insert($data, $returnID, $escape); + return parent::insert($data, $returnID); } /** * Updates a single record in the database. If an object is provided, * it will attempt to convert it into an array. * - * @param integer|array|string|null $id ID - * @param array|object|null $data Data - * @param boolean|null $escape Escape + * @param integer|array|string|null $id ID + * @param array|object|null $data Data * * @return boolean * * @throws ReflectionException */ - public function update($id = null, $data = null, ?bool $escape = null): bool + public function update($id = null, $data = null): bool { if (empty($data)) { $data = $this->tempData['data'] ?? null; - $escape = $this->tempData['escape'] ?? null; + $this->escape = $this->tempData['escape'] ?? null; $this->tempData = []; } - return parent::update($id, $data, $escape); + return parent::update($id, $data); } // endregion