Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 8 additions & 51 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
namespace CodeIgniter;

use Closure;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Exceptions\ModelException;
use CodeIgniter\I18n\Time;
use CodeIgniter\Pager\Pager;
use CodeIgniter\Validation\Validation;
use CodeIgniter\Validation\ValidationInterface;
use Config\Services;
use InvalidArgumentException;
Expand All @@ -44,8 +46,6 @@
*/
abstract class BaseModel
{
// region Properties

/**
* Pager instance.
* Populated after calling $this->paginate()
Expand Down Expand Up @@ -160,7 +160,7 @@ abstract class BaseModel
/**
* Database Connection
*
* @var object
* @var BaseConnection
*/
protected $db;

Expand Down Expand Up @@ -200,7 +200,7 @@ abstract class BaseModel
/**
* Our validator instance.
*
* @var ValidationInterface
* @var Validation
*/
protected $validation;

Expand Down Expand Up @@ -288,10 +288,6 @@ abstract class BaseModel
*/
protected $afterDelete = [];

// endregion

// region Constructor

/**
* BaseModel constructor.
*
Expand All @@ -302,12 +298,12 @@ public function __construct(ValidationInterface $validation = null)
$this->tempReturnType = $this->returnType;
$this->tempUseSoftDeletes = $this->useSoftDeletes;
$this->tempAllowCallbacks = $this->allowCallbacks;
$this->validation = $validation ?? Services::validation(null, false);
}

// endregion
/** @var Validation $validation */
$validation = $validation ?? Services::validation(null, false);

// region Abstract Methods
$this->validation = $validation;
}

/**
* Fetches the row of database
Expand Down Expand Up @@ -483,10 +479,6 @@ abstract public function countAllResults(bool $reset = true, bool $test = false)
*/
abstract public function chunk(int $size, Closure $userFunc);

// endregion

// region CRUD & Finders

/**
* Fetches the row of database
*
Expand Down Expand Up @@ -1105,10 +1097,6 @@ public function errors(bool $forceDB = false)
return $this->doErrors();
}

// endregion

// region Pager

/**
* Works with Pager to get the size and offset parameters.
* Expects a GET variable (?page=2) that specifies the page of results
Expand Down Expand Up @@ -1139,10 +1127,6 @@ public function paginate(int $perPage = null, string $group = 'default', int $pa
return $this->findAll($perPage, $offset);
}

// endregion

// region Allowed Fields

/**
* It could be used when you have to change default or override current allowed fields.
*
Expand Down Expand Up @@ -1208,10 +1192,6 @@ protected function doProtectFields(array $data): array
return $data;
}

// endregion

// region Timestamps

/**
* Sets the date or current date if null value is passed
*
Expand Down Expand Up @@ -1286,10 +1266,6 @@ protected function timeToDate(Time $value)
}
}

// endregion

// region Validation

/**
* Set the value of the skipValidation flag.
*
Expand Down Expand Up @@ -1432,7 +1408,6 @@ public function getValidationRules(array $options = []): array
// or an array of rules.
if (is_string($rules))
{
// @phpstan-ignore-next-line
$rules = $this->validation->loadRuleGroup($rules);
}

Expand Down Expand Up @@ -1487,10 +1462,6 @@ protected function cleanValidationRules(array $rules, array $data = null): array
return $rules;
}

// endregion

// region Callbacks

/**
* Sets $tempAllowCallbacks value so that we can temporarily override
* the setting. Resets after the next method that uses triggers.
Expand Down Expand Up @@ -1549,10 +1520,6 @@ protected function trigger(string $event, array $eventData)
return $eventData;
}

// endregion

// region Utility

/**
* Sets the return type of the results to be as an associative array.
*
Expand Down Expand Up @@ -1702,10 +1669,6 @@ protected function transformDataToArray($data, string $type): array
return $data;
}

// endregion

// region Magic

/**
* Provides the db connection and model's properties.
*
Expand Down Expand Up @@ -1762,10 +1725,6 @@ public function __call(string $name, array $params)
return null;
}

// endregion

// region Deprecated

/**
* Replace any placeholders within the rules with the values that
* match the 'key' of any properties being set. For example, if
Expand Down Expand Up @@ -1826,6 +1785,4 @@ protected function fillPlaceholders(array $rules, array $data): array

return $rules;
}

// endregion
}
60 changes: 5 additions & 55 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,10 @@
* - allow intermingling calls to the builder
* - removes the need to use Result object directly in most cases
*
* @property ConnectionInterface $db
*
* @mixin BaseBuilder
*/
class Model extends BaseModel
{
// region Properties

/**
* Name of database table
*
Expand Down Expand Up @@ -92,10 +88,6 @@ class Model extends BaseModel
*/
protected $escape = [];

// endregion

// region Constructor

/**
* Model constructor.
*
Expand All @@ -106,19 +98,11 @@ public function __construct(ConnectionInterface &$db = null, ValidationInterface
{
parent::__construct($validation);

if (is_null($db))
{
$this->db = Database::connect($this->DBGroup);
}
else
{
$this->db = &$db;
}
}

// endregion
/** @var BaseConnection $db */
$db = $db ?? Database::connect($this->DBGroup);

// region Setters
$this->db = &$db;
}

/**
* Specify the table associated with a model
Expand All @@ -134,10 +118,6 @@ public function setTable(string $table)
return $this;
}

// endregion

// region Database Methods

/**
* Fetches the row of database from $this->table with a primary key
* matching $id. This methods works only with dbCalls
Expand Down Expand Up @@ -283,7 +263,6 @@ protected function doInsert(array $data)
}
else
{
// @phpstan-ignore-next-line
$this->insertID = $this->db->insertID();
}
}
Expand Down Expand Up @@ -400,9 +379,7 @@ protected function doDelete($id = null, bool $purge = false)
);
}

// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
return false; // @codeCoverageIgnore
}

$set[$this->deletedField] = $this->setDate();
Expand Down Expand Up @@ -573,10 +550,6 @@ public function countAllResults(bool $reset = true, bool $test = false)
return $this->builder()->testMode($test)->countAllResults($reset);
}

// endregion

// region Builder

/**
* Provides a shared instance of the Query Builder.
*
Expand Down Expand Up @@ -651,12 +624,6 @@ public function set($key, ?string $value = '', ?bool $escape = null)
return $this;
}

// endregion

// region Overrides

// region CRUD & Finders

/**
* This method is called on save to determine if entry have to be updated
* If this method return false insert operation will be executed
Expand Down Expand Up @@ -740,10 +707,6 @@ public function update($id = null, $data = null): bool
return parent::update($id, $data);
}

// endregion

// region Utility

/**
* Takes a class an returns an array of it's public and protected
* properties as an array with raw values.
Expand All @@ -770,10 +733,6 @@ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recur
return $properties;
}

// endregion

// region Magic

/**
* Provides/instantiates the builder/db connection and model's table/primary key names and return type.
*
Expand Down Expand Up @@ -850,12 +809,6 @@ public function __call(string $name, array $params)
return $result;
}

// endregion

// endregion

// region Deprecated

/**
* Takes a class an returns an array of it's public and protected
* properties as an array suitable for use in creates and updates.
Expand Down Expand Up @@ -931,7 +884,4 @@ public static function classToArray($data, $primaryKey = null, string $dateForma

return $properties;
}

// endregion

}