ARTICLE AD BOX
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; }2,0924 gold badges50 silver badges71 bronze badges
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.
5,1482 gold badges17 silver badges39 bronze badges
Explore related questions
See similar questions with these tags.
