Skip to content

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

Closed

Description

@shishamo

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

Activity

  1. added
    bugVerified issues on the current code behavior or pull requests that will fix them
    on May 6, 2023
  2. shishamo commented on Jun 6, 2023

    @shishamo
    Author

    @kenjis is there any update to that issue?

  3. changed the title [-]Bug: cast boolean in entity from PostgreSQL is always true[/-] [+]Bug: [Entity] cast boolean in entity from PostgreSQL is always true[/+] on Jun 6, 2023
  4. kenjis commented on Jun 6, 2023

    @kenjis
    Member

    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.

  5. added
    enhancementPRs that improve existing functionalities
    and removed
    bugVerified issues on the current code behavior or pull requests that will fix them
    on Jun 6, 2023
  6. iRedds commented on Jun 6, 2023

    @iRedds
    Collaborator

    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.

  7. kenjis commented on Jun 6, 2023

    @kenjis
    Member

    @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.

  8. shishamo commented on Jun 6, 2023

    @shishamo
    Author
    <?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

  9. iRedds commented on Jun 6, 2023

    @iRedds
    Collaborator

    @kenjis I think it should be left as is.

  10. kenjis commented on Jun 6, 2023

    @kenjis
    Member

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

  11. iRedds commented on Jun 6, 2023

    @iRedds
    Collaborator

    @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.

  12. changed the title [-]Bug: [Entity] cast boolean in entity from PostgreSQL is always true[/-] [+][Entity] cast boolean in entity from PostgreSQL is always true[/+] on Jan 31, 2024
  13. kenjis commented on Feb 5, 2024

    @kenjis
    Member

    #8243 would solve this issue.

  14. changed the title [-][Entity] cast boolean in entity from PostgreSQL is always true[/-] [+][Postgre][Entity] cast boolean in entity from PostgreSQL is always true[/+] on Feb 5, 2024
  15. shishamo commented on Feb 8, 2024

    @shishamo
    Author

    Thank you @kenjis ~

  16. michalsn commented on Feb 19, 2026

    @michalsn
    Member

    Last comment 2 years ago. We can assume that casting in the model should be used instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementPRs that improve existing functionalities

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions