diff --git a/system/Exceptions/ModelException.php b/system/Exceptions/ModelException.php index 15a68b96ae6a..2d20f9e09e52 100644 --- a/system/Exceptions/ModelException.php +++ b/system/Exceptions/ModelException.php @@ -10,4 +10,9 @@ public static function forNoPrimaryKey(string $modelName) { return new static(lang('Database.noPrimaryKey', [$modelName])); } + + public static function forNoDateFormat(string $modelName) + { + return new static(lang('Database.noDateFormat', [$modelName])); + } } diff --git a/system/Language/en/Database.php b/system/Language/en/Database.php index 250550c3a96e..22d719c66771 100644 --- a/system/Language/en/Database.php +++ b/system/Language/en/Database.php @@ -26,6 +26,7 @@ 'featureUnavailable' => 'This feature is not available for the database you are using.', 'tableNotFound' => 'Table `{0}` was not found in the current database.', 'noPrimaryKey' => '`{0}` model class does not specify a Primary Key.', + 'noDateFormat' => '`{0}` model class does not have a valid dateFormat.', 'fieldNotExists' => 'Field `{0}` not found.', 'forEmptyInputGiven' => 'Empty statement is given for the field `{0}`', 'forFindColumnHaveMultipleColumns' => 'Only single column allowed in Column name.', diff --git a/system/Model.php b/system/Model.php index 821c512ecb79..2842ec3271c4 100644 --- a/system/Model.php +++ b/system/Model.php @@ -395,6 +395,7 @@ public function find($id = null) * @param string $columnName * * @return array|null The resulting row of data, or null if no data found. + * @throws \CodeIgniter\Database\Exceptions\DataException */ public function findColumn(string $columnName) { @@ -1152,6 +1153,7 @@ public function protect(bool $protect = true) * @param string $table * * @return BaseBuilder + * @throws \CodeIgniter\Exceptions\ModelException; */ protected function builder(string $table = null) { @@ -1226,8 +1228,8 @@ protected function doProtectFields(array $data): array /** * A utility function to allow child models to use the type of * date/time format that they prefer. This is primarily used for - * setting created_at and updated_at values, but can be used - * by inheriting classes. + * setting created_at, updated_at and deleted_at values, but can be + * used by inheriting classes. * * The available time formats are: * - 'int' - Stores the date as an integer timestamp @@ -1237,6 +1239,7 @@ protected function doProtectFields(array $data): array * @param integer $userData An optional PHP timestamp to be converted. * * @return mixed + * @throws \CodeIgniter\Exceptions\ModelException; */ protected function setDate(int $userData = null) { @@ -1253,6 +1256,8 @@ protected function setDate(int $userData = null) case 'date': return date('Y-m-d', $currentDate); break; + default: + throw ModelException::forNoDateFormat(get_class($this)); } } diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index b3ec505751a0..72b62e0a13fc 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1223,6 +1223,20 @@ public function testThrowsWithNoPrimaryKey() //-------------------------------------------------------------------- + /** + * @expectedException \CodeIgniter\Exceptions\ModelException + * @expectedExceptionMessage `Tests\Support\Models\UserModel` model class does not have a valid dateFormat. + */ + public function testThrowsWithNoDateFormat() + { + $model = new UserModel(); + $this->setPrivateProperty($model, 'dateFormat', ''); + + $model->delete(1); + } + + //-------------------------------------------------------------------- + public function testInsertID() { $model = new JobModel(); diff --git a/user_guide_src/source/models/model.rst b/user_guide_src/source/models/model.rst index 70b78e837543..954a9193f160 100644 --- a/user_guide_src/source/models/model.rst +++ b/user_guide_src/source/models/model.rst @@ -174,9 +174,10 @@ Leave it empty to avoid update it (even useTimestamps is enabled) **$dateFormat** -This value works with $useTimestamps to ensure that the correct type of date value gets -inserted into the database. By default, this creates DATETIME values, but valid options -are: datetime, date, or int (a PHP timestamp). +This value works with $useTimestamps and $useSoftDeletes to ensure that the correct type of +date value gets inserted into the database. By default, this creates DATETIME values, but +valid options are: datetime, date, or int (a PHP timestamp). Using 'useSoftDeletes' or +'useTimestamps' with an invalid or missing dateFormat will cause an exception. **$validationRules**