CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

[Postgre][Entity] cast boolean in entity from PostgreSQL is always true

Open shishamo opened this issue 1 year ago • 10 comments

PHP Version

8.1

CodeIgniter4 Version

4.3.4

CodeIgniter4 Installation Method

Composer (as dependency to an existing project)

Which operating systems have you tested for this bug?

macOS

Which server did you use?

apache

Database

PostgreSQL 15.2

What happened?

When try to cast boolean value in entity when use PostgreSQL the value is always true

When check the entity the value true is set as string t, and the value false is set as string f

The BooleanCast cast as (bool) $value so the value always returns true

Steps to Reproduce

class TestEntity extends BaseEntity
{
    protected $casts = [
        'bolean_value'  => 'boolean',
    ];
    protected $datamap = [
        'boleanValue' => 'bolean_value',
    ];
}
class TestModel extends APIBaseModel
{
    protected $table = 'postgresql_table';
    protected $returnType = TestEntity::class;
}
class TestController extends BaseController
{
    protected $modelName = TestModel::class;

    public function find(): Response
    {
        return $this->respond($this->model->first());
    }
}

Expected Output

{
    "boleanValue": false
}

When postgresql DB entry is false but always returns true

Anything else?

No response

shishamo avatar May 06 '23 04:05 shishamo

@kenjis is there any update to that issue?

shishamo avatar Jun 06 '23 04:06 shishamo

No. I think this is not a bug. The current implementation does not support PostgreSQL bool type. Supporting it needs an enhancement. If you need it, try to send a PR to 4.4 branch.

kenjis avatar Jun 06 '23 04:06 kenjis

You can extend the BooleanCast class and cast the type you need.

@kenjis Entity is a representation of data and is not directly related to the DBMS. I don't think this is a good solution.

iRedds avatar Jun 06 '23 04:06 iRedds

@iRedds What do you mean? You think adding PostgreBooleanCast is better? I think it is better that BooleanCast supports PostgreSQL boolean type column if possible.

kenjis avatar Jun 06 '23 04:06 kenjis

<?php

namespace App\Entities\Cast;

use CodeIgniter\Entity\Cast\BooleanCast as BaseCast;

class BooleanCast extends BaseCast
{
    public static function get($value, array $params = []): bool
    {
        if ($value === 't') {
            return true;
        }
        if ($value === 'f') {
            return false;
        }

        return parent::get($value, $params);
    }
}

Actually this is what i have done in my own extended cast.

But i was wondering why it is not handled by CI by default because the Postgre driver exists

shishamo avatar Jun 06 '23 05:06 shishamo

@kenjis I think it should be left as is.

iRedds avatar Jun 06 '23 08:06 iRedds

@iRedds CI4 supports PostgreSQL but the Entity cannot support boolean type column. Isn't it a missing feature?

kenjis avatar Jun 06 '23 10:06 kenjis

@kenjis But Entity is not part of the database layer. And the desired result can be obtained through a getter or custom type casting. Now, if we didn’t have the opportunity to get the desired result, then this would be a missing function.

iRedds avatar Jun 06 '23 14:06 iRedds

#8243 would solve this issue.

kenjis avatar Feb 05 '24 06:02 kenjis

Thank you @kenjis ~

shishamo avatar Feb 08 '24 09:02 shishamo