From d1002d9bff0e36c362c81b907a73c5926870d87c Mon Sep 17 00:00:00 2001 From: MGatner Date: Wed, 12 Jun 2019 12:04:32 -0400 Subject: [PATCH 1/7] Add exception for missing/invalid $dateFormat --- system/Model.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/system/Model.php b/system/Model.php index 821c512ecb79..e87dbbad62e3 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) { @@ -947,8 +948,8 @@ public function purgeDeleted() } return $this->builder() - ->where($this->table . '.' . $this->deletedField . ' IS NOT NULL') - ->delete(); + ->where($this->deletedField . ' IS NOT NULL') + ->delete(); } //-------------------------------------------------------------------- @@ -981,7 +982,7 @@ public function onlyDeleted() $this->tempUseSoftDeletes = false; $this->builder() - ->where($this->table . '.' . $this->deletedField . ' IS NOT NULL'); + ->where($this->deletedField . ' IS NOT NULL'); return $this; } @@ -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)); } } @@ -1543,7 +1548,7 @@ public function countAllResults(bool $reset = true, bool $test = false) { if ($this->tempUseSoftDeletes === true) { - $this->builder()->where($this->table . '.' . $this->deletedField, null); + $this->builder()->where($this->deletedField, null); } return $this->builder()->countAllResults($reset, $test); From e516ac3c8bdd4cecf617979aefea0629d0bb5f7c Mon Sep 17 00:00:00 2001 From: MGatner Date: Wed, 12 Jun 2019 12:05:08 -0400 Subject: [PATCH 2/7] Add text for missing/invalid model dateFormat --- system/Language/en/Database.php | 1 + 1 file changed, 1 insertion(+) 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.', From ef049573bf1c5f278e7aec9ff317adb9f7cdd379 Mon Sep 17 00:00:00 2001 From: MGatner Date: Wed, 12 Jun 2019 12:05:46 -0400 Subject: [PATCH 3/7] Add exception for missing/invalid dateFormat --- system/Exceptions/ModelException.php | 5 +++++ 1 file changed, 5 insertions(+) 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])); + } } From a3e5db34c93cc01bc25251b5f9a4cddbf1f55c86 Mon Sep 17 00:00:00 2001 From: MGatner Date: Wed, 12 Jun 2019 12:09:58 -0400 Subject: [PATCH 4/7] Add test for missing/invalid model dateFormat --- tests/system/Database/Live/ModelTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index b3ec505751a0..669cb54bde09 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->find(1); + } + + //-------------------------------------------------------------------- + public function testInsertID() { $model = new JobModel(); From f5d26142d38025a8eaa4d351d26ab1bd13f1f8e5 Mon Sep 17 00:00:00 2001 From: MGatner Date: Wed, 12 Jun 2019 12:15:14 -0400 Subject: [PATCH 5/7] Note dateFormat dependency --- user_guide_src/source/models/model.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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** From 4d358c83516bc8b31c6f87b4934b4c61e20ea3af Mon Sep 17 00:00:00 2001 From: MGatner Date: Wed, 12 Jun 2019 12:18:42 -0400 Subject: [PATCH 6/7] Restore deleted_at table prefixes (curses!) --- system/Model.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/system/Model.php b/system/Model.php index e87dbbad62e3..2842ec3271c4 100644 --- a/system/Model.php +++ b/system/Model.php @@ -948,8 +948,8 @@ public function purgeDeleted() } return $this->builder() - ->where($this->deletedField . ' IS NOT NULL') - ->delete(); + ->where($this->table . '.' . $this->deletedField . ' IS NOT NULL') + ->delete(); } //-------------------------------------------------------------------- @@ -982,7 +982,7 @@ public function onlyDeleted() $this->tempUseSoftDeletes = false; $this->builder() - ->where($this->deletedField . ' IS NOT NULL'); + ->where($this->table . '.' . $this->deletedField . ' IS NOT NULL'); return $this; } @@ -1548,7 +1548,7 @@ public function countAllResults(bool $reset = true, bool $test = false) { if ($this->tempUseSoftDeletes === true) { - $this->builder()->where($this->deletedField, null); + $this->builder()->where($this->table . '.' . $this->deletedField, null); } return $this->builder()->countAllResults($reset, $test); From f66a1f3676044de86ad8ce35b1e62a741f4eee65 Mon Sep 17 00:00:00 2001 From: MGatner Date: Wed, 12 Jun 2019 12:49:43 -0400 Subject: [PATCH 7/7] Change dateFormat test method to `delete` --- tests/system/Database/Live/ModelTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 669cb54bde09..72b62e0a13fc 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1232,7 +1232,7 @@ public function testThrowsWithNoDateFormat() $model = new UserModel(); $this->setPrivateProperty($model, 'dateFormat', ''); - $model->find(1); + $model->delete(1); } //--------------------------------------------------------------------