spadefoot / kohana-orm-leap

An ORM module for the Kohana PHP framework that is designed to work with all major databases.
http://spadefoot.github.io/kohana-orm-leap/
100 stars 25 forks source link

bigint support in Base_DB_ORM_Field_Integer #47

Closed taai closed 12 years ago

taai commented 12 years ago

Support for 8 byte integer is needed!

As for now there is upper_bound set to maximum 4 byte integer value (2147483647):

<?php
// line 48
$this->metadata['range']['upper_bound'] = 2147483647;

Event if I do set max_length to 19 (not 10 or 11 as in examples), the upper_bound still is 2147483647, not 9223372036854775807.

Of course, you have to keep in mind that bigint is supported only by 64bit systems, but there should be a solution!

Possible solutions: Throw an exception

<?php
if (intval($this->metadata['max_length']) > 10 AND PHP_INT_SIZE!==8)
{
    // TO DO: find more appropriate exception class and write a better message
    throw new Exception('The system does not support 8 byte integers');
}

Or leave value as string, if max_length is set to be larger than 10.

Update As I can see in the code, that upper_bound is a problem only for validation and the values will be converted to 8 byte integers (if system supports that), because nothing more than intval() function is used.

Any comments/ideas?

bluesnowman commented 12 years ago

There are two possible solutions that I can think of right now although I am sure there may be other solutions as well. Nonetheless, I am thinking that maybe it makes sense to create a new field class for big integers, which would utilize maybe Pear's Math_BigInteger class.

The other solution (or temporary workaround) is to use the String field class. Although this won't necessarily provide validation, it would allow you to read and write to the database.

taai commented 12 years ago

I made a modified version of the DB_ORM_Field_Integer : https://gist.github.com/3215176

What it does is when possible, use integers, but if system doesn't support 8byte integers and the value exceeds the range of 4byte integers, it uses strings.

This is workaround for systems that doesn't support 8byte integers. And if you are using only 4byte integers or your system supports 8byte integers, it will make no difference. Also, if the integer will be in the range between -2147483648 and 2147483647, it will be converted to ineger.

Crazy, right?! :)

So, what do you think about this solution? I could make a pull request or you can just pull the code from gist. Tell me.

bluesnowman commented 12 years ago

Just merged these changes into Leap.