Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: [DatetimeCast] Invalid parameter: nullable #8860

Closed
neznaika0 opened this issue May 6, 2024 · 3 comments · Fixed by #8870
Closed

Bug: [DatetimeCast] Invalid parameter: nullable #8860

neznaika0 opened this issue May 6, 2024 · 3 comments · Fixed by #8870
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@neznaika0
Copy link
Contributor

PHP Version

8.3

CodeIgniter4 Version

4.5.1

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

cli-server (PHP built-in webserver)

Database

MariaDB 10

What happened?

Conversion to date does not work.

Steps to Reproduce

Add Model:

class UserModel extends Model
{
    protected $table         = 'user';
    protected $primaryKey    = 'id';
    protected $allowedFields = [
        'first_name',
        'date_birth'
    ];
    protected $returnType     = User::class;
    protected array $casts = [
        'id' => '?int',
        'date_birth' => '?datetime'
    ];
}

Select row from DB (get User object):
$this->userModel->get($userId);
See error:

InvalidArgumentException
Invalid parameter: nullable search →

SYSTEMPATH/DataCaster/Cast/DatetimeCast.php at line 50

Expected Output

Return Datetime object or null

Anything else?

I think it's worth considering $params[0] = 'nullable' state
https://github.com/codeigniter4/CodeIgniter4/blob/84e06d06b67b0bc8bea71dfa24f93264eec50b19/system/DataCaster/Cast/DatetimeCast.php#L46C1-L51

        $format = match ($params[0] ?? '') {
            ''      => $helper->dateFormat['datetime'],
            // 'nullable' => $helper->dateFormat['datetime'],
            'ms'    => $helper->dateFormat['datetime-ms'],
            'us'    => $helper->dateFormat['datetime-us'],
            default => throw new InvalidArgumentException('Invalid parameter: ' . $params[0]),
        };
@neznaika0 neznaika0 added the bug Verified issues on the current code behavior or pull requests that will fix them label May 6, 2024
@neznaika0
Copy link
Contributor Author

It would also be nice to add other time types for DatetimeCast.

'date'        => 'Y-m-d',
'time'        => 'H:i:s',

@neznaika0
Copy link
Contributor Author

neznaika0 commented May 6, 2024

In the process, a question:
If $attributes ($original) if the entities have a string value, then casting fails. DatetimeCast expects an object, but actually contains $attributes['date_birth'] = '2001-05-03 19:56:00

[CodeIgniter\DataCaster\Cast\DatetimeCast] Invalid value type: string, and its value: '2001-05-03 19:56:00'

 App\Models\Entity\User {#160 ▼
  #datamap: []
  #dates: []
  #casts: []
  #castHandlers: []
  -defaultCastHandlers: array:15 [▶]
  #attributes: array:22 [▼
    "id" => "1"
    "first_name" => "Louvenia"
    "date_birth" => "2001-05-03 19:56:00"
  ]

UPD: I did some tests and realized that the new behavior of $attributes should have new types instead of primitive strings.

@kenjis
Copy link
Member

kenjis commented May 8, 2024

Try #8870

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
2 participants