diff --git a/system/BaseModel.php b/system/BaseModel.php index c4006afc0a51..922dd65a51d3 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -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; @@ -44,8 +46,6 @@ */ abstract class BaseModel { - // region Properties - /** * Pager instance. * Populated after calling $this->paginate() @@ -160,7 +160,7 @@ abstract class BaseModel /** * Database Connection * - * @var object + * @var BaseConnection */ protected $db; @@ -200,7 +200,7 @@ abstract class BaseModel /** * Our validator instance. * - * @var ValidationInterface + * @var Validation */ protected $validation; @@ -288,10 +288,6 @@ abstract class BaseModel */ protected $afterDelete = []; - // endregion - - // region Constructor - /** * BaseModel constructor. * @@ -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 @@ -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 * @@ -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 @@ -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. * @@ -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 * @@ -1286,10 +1266,6 @@ protected function timeToDate(Time $value) } } - // endregion - - // region Validation - /** * Set the value of the skipValidation flag. * @@ -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); } @@ -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. @@ -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. * @@ -1702,10 +1669,6 @@ protected function transformDataToArray($data, string $type): array return $data; } - // endregion - - // region Magic - /** * Provides the db connection and model's properties. * @@ -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 @@ -1826,6 +1785,4 @@ protected function fillPlaceholders(array $rules, array $data): array return $rules; } - - // endregion } diff --git a/system/Model.php b/system/Model.php index aa774b066e00..3aca64882e03 100644 --- a/system/Model.php +++ b/system/Model.php @@ -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 * @@ -92,10 +88,6 @@ class Model extends BaseModel */ protected $escape = []; - // endregion - - // region Constructor - /** * Model constructor. * @@ -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 @@ -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 @@ -283,7 +263,6 @@ protected function doInsert(array $data) } else { - // @phpstan-ignore-next-line $this->insertID = $this->db->insertID(); } } @@ -400,9 +379,7 @@ protected function doDelete($id = null, bool $purge = false) ); } - // @codeCoverageIgnoreStart - return false; - // @codeCoverageIgnoreEnd + return false; // @codeCoverageIgnore } $set[$this->deletedField] = $this->setDate(); @@ -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. * @@ -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 @@ -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. @@ -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. * @@ -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. @@ -931,7 +884,4 @@ public static function classToArray($data, $primaryKey = null, string $dateForma return $properties; } - - // endregion - }