Skip to content

Commit

Permalink
Merge pull request #4072 from najdanovicivan/nosql/base-model-escape
Browse files Browse the repository at this point in the history
BaseModel/Model - Attempt to rework escape parameter
  • Loading branch information
michalsn authored Jan 8, 2021
2 parents eb0cf46 + 210a3ab commit d09a546
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
19 changes: 8 additions & 11 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 23 additions & 13 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -655,46 +667,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
Expand Down

0 comments on commit d09a546

Please sign in to comment.