I have a varbinary(200) NOT NULL
field on a table.
Let’s say the table is called users
and the field is called secret
.
In CakePHP 3.X, accessing
$user->secret;
after retrieving one row of the table would give me a string.
In CakePHP 4.X, instead, accessing the same property gives me a resource
.
If I do
stream_get_contents($user->secret);
I can then access the string value, but why do I have do that now? I couldn’t find anything specific in the CakePHP changelogs pertaining to varbinary
fields.
It’s quite annoying now to have to go through all the places in my code where I access that property, and add stream_get_contents()
everywhere.
Is there any way I can change my model instead so that the behavior remains consistent with what it was before?
2
Answers
I’m not familiar with the cake fw itself, but it looks like you can use the accessors in entities so you can alter the return when retrieving the property:
Example taken from the link above.
Binary data columns were always meant to return resources in
3.x
, but you’re probably using a very old 3.x version, whereVARBINARY
wasn’t recognized (pre3.6.12
), resulting in it to default to treating it as a string.Binary data columns are generally returned as resources in order to unify the behavior across the different supported DBMS and their various binary data types, as for some of them PDO returns the data as strings, and for others it returns it as resource handles.
Using entity accessors as suggested by @manuel-guzman is a way to always retrieve strings, but be aware that accessors will be used when persisting entities, meaning they will be invoked when saving, and the data they return is what will end up in the database!
Another option would be result formatters:
See also
And there’s also custom database types that could be used to always return the data as strings when reading from the database using the query builder:
See also