bockzior
bockzior

Reputation: 536

string converted to int is too large

I'm using php-activerecord to manage my MySQL entries.

The block of code where the problem ocurs is the following:

$item->id = $result->id;
$item->save();

var_dump($result->id); // string(10) "2386737351" 
var_dump($item->id);   // int(2147483647)

The problem is that $result->id is string(10) "2386737351" like it should, but $item->id becomes int(2147483647). My column id is of type BIGINT, so no problem there.

It looks like php-activerecord converts that value to a int again which is capped at it's maximum value of 2147483647.

Obviously this is terribly wrong and causes Duplicate entry '2147483647' for key 'unique'.

How can I overcome this?

Upvotes: 2

Views: 229

Answers (2)

Fraligatorus
Fraligatorus

Reputation: 69

Native 64-bit integers require 64-bit hardware AND the 64-bit version of PHP.

On 32-bit hardware: $ php -r 'echo PHP_INT_MAX;' 2147483647

On 64-bit hardware: $ php -r 'echo PHP_INT_MAX;' 9223372036854775807

As said by scotts on this question : how to have 64 bit integer on PHP?

So you have to use PHP 64bits version to use BIGINTs, else they will be automatically (or not and you will get an error) converted to integer.

Upvotes: 1

Nanne
Nanne

Reputation: 64399

Your BIGINT is being cast to an int because of the setting in column.php. While the "real solution" would be to run a 64 bit system, if you cannot, the other thing might be to just stop the cast from happening

Look at line 37 in column.php , I suggest you change that. I haven't tried it myself, but I suppose that this does the trick:

'bigint'    => self::STRING,

Upvotes: 1

Related Questions