Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
The integer id field is being populated as a string after save #13002
Expected and Actual Behavior
When I save a model, the id field is automatically populated with the id value, however, instead of it being an integer value, it's populated as a string value. When outputting json this is an issue. In the database, the id field is an
e.g. instead of seeing:
Yes, good catch. We need to respect column map types and cast appropriately right here as MySQL lastInsertId() always returns string
Always returns string
for now solved it by overriding model's toArray
The proper solution is to just cast to (int) here:
PHP returns string from MySQL's internal call which returns bigint. There's no reason why we shouldn't just cast to int in the code ...
I've been talking about this with the core team.
The issues at play are:
If either if the above are true. I believe ... that we should properly handle for the following PDO extensions:
Thus, with respect to MySQL/MariaDB ... lastInsertId() should always be sufficient to fit inside a PHP 64bit integer. However, we can't do this for 100% of the cases (e.g. users that don't fit into the top two bullets listed above).
So we could introduce a new global value
I guess the other thought is we could pull the column map for the field and if it's INT and
Thus, the reason for the explicit and separate global which removes any crazy logic in terms of attempting to autodetect how to handle this case.
Thus, potential solution would be a 3 line change with effectively zero performance impact. For example: