I’ve generated a controller, table, and entity using bake against a table, and done some modifications to the entity to allow me to manually insert the entity ID. I originally created the id as an integer, but the system that generates the ids for me is producing numbers that require me to use a bigint, rather than an int.
That’s easy enough to do in the DB, but somewhere Cake is still trying to insert the largest possible integer (2147483647) rather than the larger integer the system is handing me. Is it possible to remap the Table to understand that id is now a bigint, rather than an int, without having to rebake the objects?
That is interesting! But it didn’t work. Here’s an example of what I’m talking about.
The DB
CREATE TABLE public.schedules
(
id bigint NOT NULL,
job_id integer,
user_id integer,
start_date timestamp without time zone,
end_date timestamp without time zone,
CONSTRAINT pk_schedules_id PRIMARY KEY (id),
)
The insert request data
{
"id":"1475881942413", -- This is a bigint
"job_id":"19",
"user_id":"26",
"start_date":"2017-02-22 08:10",
"end_date":"2017-02-22 12:35"
}
So after a bit of digging, I think this is actually a limitation on PHP’s integer size on windows platforms. I just created a simple test controller from scratch against a simple table with a bigint defined id column, and it demonstrates the exact same behavior. The debugger SQL log shows the insertion as
INSERT INTO tests (id, description)
VALUES
(
2147483647, 'This better be a big damn int.'
) RETURNING *
While the request data clearly shows
id: 1475881942413
description: This better be a big damn int.
The PHP docs tell me
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that’s 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit
Since I’m developing on a windows platform, PHP ints are always going to be 32 bits, which sucks.
How hard is it to upgrade to PHP 7 underneath Cake?
Unfortunately, that’s not really an option for me. I ended up changing the data type to varchar so it treats the ID like a string. Now I’m in typecasting hell.