Merge pull request #10009 from creative-commoners/pulls/4.8/treedropdown-pdo

FIX Cast DBInt value to int
This commit is contained in:
Steve Boyd 2021-07-07 12:21:50 +12:00 committed by GitHub
commit 2e5908cf21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -20,6 +20,15 @@ class DBInt extends DBField
parent::__construct($name); parent::__construct($name);
} }
/**
* Ensure int values are always returned.
* This is for mis-configured databases that return strings.
*/
public function getValue()
{
return (int) $this->value;
}
/** /**
* Returns the number, with commas added as appropriate, eg “1,000. * Returns the number, with commas added as appropriate, eg “1,000.
*/ */

View File

@ -0,0 +1,18 @@
<?php
namespace SilverStripe\ORM\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\FieldType\DBInt;
class DBIntTest extends SapphireTest
{
public function testGetValueCastToInt()
{
$field = DBInt::create('MyField');
$field->setValue(3);
$this->assertSame(3, $field->getValue());
$field->setValue('3');
$this->assertSame(3, $field->getValue());
}
}