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: The selected data type from Entity or QueryBuilder is defferent from DB field type. #7259

Closed
kai890707 opened this issue Feb 16, 2023 · 5 comments

Comments

@kai890707
Copy link
Contributor

kai890707 commented Feb 16, 2023

PHP Version

8.1

CodeIgniter4 Version

4.1.5

CodeIgniter4 Installation Method

Git

Which operating systems have you tested for this bug?

Linux

Which server did you use?

cli

Database

MariaDB 10.1

What happened?

The data type selected is different from the DB field type.
For example, if you use a migrate file to define a field type as INTEGER or FLOAT, and then execute php spark migrate on the database to create a table, the data type returned using Query Builder will be different from the type initially set.

Steps to Reproduce

  1. Use Migration make a migrate file.
  2. addField into mugrate file.
          $this->forge->addField([
            'is_string'    => [
                'type'           => 'VARCHAR',
                'constraint'     => 30,
                'comment'        => "is_string"
            ],
            'is_integer'   => [
                'type'           => 'INT',
                'constraint'     => 30,
                'comment'        => "is_integer"
            ],
            'is_float'   => [
                'type'           => 'FLOAT',
                'comment'        => "is_float"
            ],
        ]);
        $this->forge->addKey('is_string', true);
        $this->forge->createTable('CastTable');
  1. Use db query in Controller to get data
$db = \Config\Database::connect();
$queryData = $db->table('CastTable')->get()->getResult();
var_dump($queryData);

4.Get data of the wrong type

array(1) {
  [0]=>
  object(stdClass)#57 (3) {
    ["is_string"]=>
    string(17) "Hi CodeIgniter4 !"
    ["is_integer"]=>
    string(3) "999"
    ["is_float"]=>
    string(5) "9.999"
  }
}

Expected Output

array(1) {
  [0]=>
  object(stdClass)#57 (3) {
    ["is_string"]=>
    string(17) "Hi CodeIgniter4 !"
    ["is_integer"]=>
    int(999)
    ["is_float"]=>
    float(9.999)
  }
}

Anything else?

No response

@kai890707 kai890707 added the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 16, 2023
@kenjis kenjis removed the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 16, 2023
@kai890707
Copy link
Contributor Author

I finished fixing the errors in this section and I can send a PR.

@kenjis
Copy link
Member

kenjis commented Feb 16, 2023

This is not a bug in CodeIgniter.
It is the specification of PHP database module like MySQLi.

@kai890707
Copy link
Contributor Author

So does this need to be adjusted by the user? Or can CodeIgniter 4 add this part of the fix?

@tangix
Copy link
Contributor

tangix commented Feb 18, 2023

So does this need to be adjusted by the user? Or can CodeIgniter 4 add this part of the fix?

You could use CI's Model with Entity providing the casts needed.

@kenjis
Copy link
Member

kenjis commented Feb 22, 2023

Closed by #7265

@kenjis kenjis closed this as completed Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants