-
- 17 Posts
I traced a defect in my application to the use of PHP’s intval() function used by xPDO when returning MySQL integer values from database - my BIGINT values were coming back as 2,147,483,647 (the maximum 32-bit signed integer) which was the giveaway.
The maximum value returnable by intval() on a 32-bit platform is 2,147,483,647 (2^31-1) or double that for unsigned types, so if you are storing any numbers larger than that (in columns with type BIGINT(10+) or INT(10+)) you will have issues.
On 64-bit systems you may still not be safe, as the maximum signed integer supported is 9,223,372,036,854,775,807 or double for unsigned. Hence some numbers in BIGINT(19) and BIGINT(20) may be affected. Anything smaller will be fine.
Luckily in this particular case, I don’t actually perform any arithmetic on the numbers I’m storing (they’re identifiers only) so I’m updating the affected columns from BIGINT to VARCHAR type.. I can still sort and compare them alphanumerically so that’s all I’m worried about.
If you absolutely must work with very large integers, there are PHP libraries providing large number support. To overcome the storage and retrieval issue in xPDO, you’d have to:
a) Wait for a newer version of PHP that natively supports large numbers in intval() or
b) Modify xPDO source to replace or wrap the use of intval() to work with large numbers as strings or
c) Store your large numbers as strings in the first place, and then you can use your favourite PHP large number library in your app logic or
d) Do nothing but ensure you will never overflow the maximum signed or unsigned integer value you’re storing in INT or BIGINT MySQL type
Cheers
-
- 4,149 Posts
SD:
Thanks for bringing this up. We’ll look into this.
shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect |
github |
splittingred.com
All you need to do is define BIGINT columns in your xPDO schema with phptype="string" instead of phptype="integer" and xPDO will treat your values as strings, completely avoiding the problem.