gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.3k stars 279 forks source link

Redbean returning boolean columns as string #944

Open noorbakerally opened 4 weeks ago

noorbakerally commented 4 weeks ago

I am having a problem using Redbean. I have a table photos having the photos definition:

+----------------+-------------+------+-----+---------+----------------+
| Field          | Type        | Null | Key | Default | Extra          |
+----------------+-------------+------+-----+---------+----------------+
| id             | int(11)     | NO   | PRI | NULL    | auto_increment |
| account_number | varchar(20) | NO   |     | NULL    |                |
| photo          | text        | YES  |     | NULL    |                |
| isDefault      | tinyint(1)  | YES  |     | 0       |                |
+----------------+-------------+------+-----+---------+----------------+

I have a small PHP script:

<?php
        require 'rbconnect.php';
        $photos = R::findOne('photos');
        var_dump($photos);
        $photos->id = intval($photos->id);
        $photos->isDefault = intval($photos->isDefault);
        echo json_encode($photos);
?>

First part of returned result var_dump($photos):

object(RedBeanPHP\OODBBean)#17 (11) {
  ["properties":protected]=>
  array(4) {
    ["id"]=>
    string(1) "1"
    ["account_number"]=>
    string(10) "1234567890"
    ["photo"]=>
    string(17) "sample_photo_data"
    ["isDefault"]=>
    string(1) "0"
  }
  ["__info":protected]=>
  array(8) {
    ["type"]=>
    string(6) "photos"
    ["sys.id"]=>
    string(2) "id"
    ["sys.orig"]=>
    array(4) {
      ["id"]=>
      string(1) "1"
      ["account_number"]=>
      string(10) "1234567890"
      ["photo"]=>
      string(17) "sample_photo_data"
      ["isDefault"]=>
      string(1) "0"
    }
    ["tainted"]=>
    bool(false)
    ["changed"]=>
    bool(false)
    ["changelist"]=>
    array(0) {
    }
    ["model"]=>
    NULL
    ["data.bundle"]=>
    array(0) {
    }
  }
  ["beanHelper":protected]=>
  object(RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper)#13 (0) {
  }
  ["fetchType":protected]=>
  NULL
  ["withSql":protected]=>
  string(0) ""
  ["withParams":protected]=>
  array(0) {
  }
  ["aliasName":protected]=>
  NULL
  ["via":protected]=>
  NULL
  ["noLoad":protected]=>
  bool(false)
  ["all":protected]=>
  bool(false)
  ["castProperty":protected]=>
  NULL
}

Second part of the returned json is: {"id":1,"account_number":"1234567890","photo":"sample_photo_data","isDefault":"0","is_default":0}

I am having two ambiguities:

  1. If I do not do a casting both id and isDefault returned type is String, are not they supposed to be of type int?
  2. Casting is working for id but not for isDefault. When casting isDefault, it is instead recreating another column is_default.

Noor

gabordemooij commented 2 weeks ago

Sorry for the late reply. Have you tried:

R::getDatabaseAdapter()->getDatabase()->stringifyFetches( FALSE );