michalsn / codeigniter4-uuid

UUID package for CodeIgniter 4 with support for Model and Entity.
MIT License
41 stars 9 forks source link

Bug: Argument passed to Michalsn\Uuid\Uuid::fromString() must be of the type string, null given. #5

Closed yassinedoghri closed 3 years ago

yassinedoghri commented 3 years ago

Hey @michalsn!

I'm getting this error:

Argument 1 passed to Michalsn\Uuid\Uuid::fromString() must be of the type string, null given, called in /castopod-host/vendor/michalsn/codeigniter4-uuid/src/UuidModel.php on line 381

This happens when trying to insert a record with a null value in a Uuid field that should be converted in bytes. Here is the line in question, in the doInsert method:

if (in_array($key, $this->uuidFields) && $this->uuidUseBytes === true)
{
    $val = ($this->uuid->fromString($val))->getBytes(); // line 381, $val is null
}

Given that you may have a Uuid field with possible null values, there should be a check to prevent this from happening.

Possible solution

Add a condition to check that the field value is not null for insert and update:

if ($val && in_array($key, $this->uuidFields) && $this->uuidUseBytes === true)
{
    $val = $this->uuid->fromString($val)->getBytes();
}