I created a REST API in CakePHP 4.5, and all the ID fields are of type Bigint. Is there a way to force CakePHP to convert these fields to strings before returning the JSON?

There are many fields, and it would be impractical to edit each possible return function. I thought about defining this conversion in the EntityTable or the model.

Is that possible?

I tried this, but cake still returning id as INT

public function getSchema():TableSchema{ $schema = parent::getSchema(); $schema->setColumnType('id', 'string'); return $schema; }

agilgur5's user avatar

agilgur5

2,0924 gold badges50 silver badges71 bronze badges

Daniel Girardi's user avatar

You could create virtual fields for those, add the original fields to the _hidden array in the entities, and the new virtual fields to the _virtual array there. Depending on how many "many fields" is, this may or may not be viable for you.

If that's too much, I would look at creating an AppEntity that all of your entities extend instead of the base one, and override the get function from EntityTrait there, calling the parent implementation, then casting to a string if necessary before returning it.

Greg Schmidt's user avatar

1 Comment

Thx for the insight Greg. I override the get functions and Works Fine.

2025-11-23T02:20:33.46Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.