Skip to content

Commit

Permalink
Merge pull request #2054 from MGatner/dateformat-exception
Browse files Browse the repository at this point in the history
Add model exceptions for missing/invalid dateFormat
  • Loading branch information
lonnieezell authored Jun 12, 2019
2 parents 8ae976f + f66a1f3 commit 2f65567
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions system/Exceptions/ModelException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
}
1 change: 1 addition & 0 deletions system/Language/en/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
9 changes: 7 additions & 2 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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)
{
Expand All @@ -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));
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/system/Database/Live/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions user_guide_src/source/models/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down

0 comments on commit 2f65567

Please sign in to comment.