Skip to content

Fix insert() TypeError when primary key is typed as int#34

Closed
enlivenapp wants to merge 1 commit intoflightphp:masterfrom
enlivenapp:fix/insert-lastInsertId-type
Closed

Fix insert() TypeError when primary key is typed as int#34
enlivenapp wants to merge 1 commit intoflightphp:masterfrom
enlivenapp:fix/insert-lastInsertId-type

Conversation

@enlivenapp
Copy link
Copy Markdown
Contributor

I created a subclass with public int $id and inserted a new record:

class User extends \flight\ActiveRecord {
    public function __construct($pdo = null) {
        parent::__construct($pdo, 'users');
    }
    public int $id;
    public string $name;
}

$user = new User($pdo);
$user->name = 'John';
$user->insert();

I expected the insert to succeed and $user->id to be set to the new auto-increment value.

Instead, it throws TypeError: Cannot assign string to property User::$id of type int, because lastInsertId() returns a string.

Subclasses without a typed primary key are not affected.

Tests included. All existing tests continue to pass.

lastInsertId() returns a string, which throws a TypeError when assigned
to a typed public int property under strict_types. Cast the value to int
when the primary key property has an int type declaration.

enlivenapp <mw@enlivenapp.com>
@enlivenapp
Copy link
Copy Markdown
Contributor Author

enlivenapp commented Apr 24, 2026

Closed, too much separation. see #33 . Sorry for the noise.

@enlivenapp enlivenapp closed this Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant