gabordemooij / redbean

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

Upon reaching the number 255, an error occurs and the type does not change! #911

Closed iyamk closed 1 year ago

iyamk commented 1 year ago

I write statistics to the database

$v = R::dispense('visit');
$v->count = 1;
$v->visit_date = date('Y-m-d');
R::store($v);

Type in table becomes tinyint. When I visit the page, I update the statistics

$v = R::findOne('visit', ' visit_date = CURDATE() ');
if ($v)
{
    $v->count = $v->count + 1;
    R::store($v);
}

And I see an error when the number exceeds 255

Fatal error: Uncaught [22003] - SQLSTATE[22003]: Numeric value out of range: 1264 Out of range value for column 'count' at row 1 trace: #0 /home/user/app/rb-mysql.php(1257): RedBeanPHP\Driver\RPDO->runQuery() #1 /home/user/app/rb-mysql.php(4363): RedBeanPHP\Driver\RPDO->Execute() #2 /home/user/app/rb-mysql.php(6361): RedBeanPHP\Adapter\DBAdapter->exec() #3 /home/user/app/rb-mysql.php(8974): RedBeanPHP\QueryWriter\AQueryWriter->updateRecord() #4 /home/user/app/rb-mysql.php(8523): RedBeanPHP\Repository\Fluid->storeBean() #5 /home/user/app/rb-mysql.php(9634): RedBeanPHP\Repository->store() #6 /home/user/app/rb-mysql.php(12598): RedBeanPHP\OODB->store() #7 /home/user/app/index.php(26): RedBeanPHP\Facade::store() #8 {main} thrown in /home/user/app/rb-mysql.php on line 838

Why doesn't the type change automatically? I read the documentation and there is not a word about how to assign the necessary types forever

marios88 commented 1 year ago

I think if you have set your database fields by hand rb does not change them

255 is the limit for unsigned TINYINT

Also check if you are in fluid mode

iyamk commented 1 year ago

I came up with a wrapper for creating the base and now the problem is solved:

if (is_null(R::findOne('visit')))
{
    $t = R::dispense('visit');
    $t->ip = '255.255.255.255';
    $t->count = 1000000000;
    $t->visit_date = date('Y-m-d');
    R::store($t);
    R::trash($t);
}
R::freeze(true);

This is much more convenient than constantly climbing into the database after deleting it.