spotorm / spot2

Spot v2.x DataMapper built on top of Doctrine's Database Abstraction Layer
http://phpdatamapper.com
BSD 3-Clause "New" or "Revised" License
601 stars 101 forks source link

Integer primary key of entity is returned as string when created #286

Closed bikalbasnet closed 5 years ago

bikalbasnet commented 5 years ago

Steps to Reproduce Let's say we have an entity whose primary key is integer.

...
public static function fields()
    {
        return [
            'id'           => ['type' => 'integer', 'autoincrement' => true, 'primary' => true],
            'username'     => ['type' => 'string', 'required' => true],
        ];
    }
...

and now we create a new record for this entity

$entity = $mapper->create([
    'name' => 'Chester Tester',
    'username' => 'chester'
]);

var_dump($entity);

Expected Result The primary key should be integer 1 not string "1"

---
 _data: array:14 [
    "id" => 1
....

Actual Result But we are getting primary key as string 1

---
 _data: array:14 [
    "id" => "1"
....

Investigation Upon some Investigation I found the following code on https://github.com/spotorm/spot2/blob/master/lib/Mapper.php#L769

$result = $connection->lastInsertId($sequenceName);
$entity->$pkField = $result;

But after this function is called shouldn't we type cast the value into integer if the type of primary key is integer with something like this atleast?

$entity->$pkField = ($pkFieldInfo['type'] === "integer") ? (integer) $result : $result;