Skip to content

Commit

Permalink
Merge pull request #10009 from creative-commoners/pulls/4.8/treedropd…
Browse files Browse the repository at this point in the history
…own-pdo

FIX Cast DBInt value to int
  • Loading branch information
emteknetnz authored Jul 7, 2021
2 parents e8c14a9 + 87d076f commit 2e5908c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ORM/FieldType/DBInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public function __construct($name = null, $defaultVal = 0)
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”.
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/php/ORM/DBIntTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 2e5908c

Please sign in to comment.