gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.31k stars 279 forks source link

Method RedBeanPHP\OODBBean::__toString() must return a string value #528

Closed agildehaus closed 7 years ago

agildehaus commented 8 years ago

In Repository::processGroups():

return array(
    array_merge( $additions, array_diff( $current, $originals ) ),
    array_merge( $trashcan, array_diff( $originals, $current ) ),
    array_merge( $residue, array_intersect( $current, $originals ) )
);

The $current OODBBean can possibly contain binary data if it was retrieved from the database. Mine happens to have Well-Known-Binary from a geometry field.

array_diff calls OODBBean::_toString(). That method attempts to call the model's __toString(), but falls back to json_encode to serialize if the model does not have a __toString().

Unfortunately json_encode can and will fail with binary data. json_encode returns false, which becomes the return value of OODBBean::__toString(), which causes array_diff to throw an exception (see subject).

I'm currently patching this by calling utf8_encode() on each OODBBean property in OODBBean::__toString() before json_encode is called. Seems to work well. Not sure if it's the best solution.

nodefish commented 7 years ago

Thanks for posting this. I've been encountering this error in a fairly complex project without explicitly storing/retrieving binary data and I haven't been able to diagnose it thus far. I'll try using utf8_encode.

gabordemooij commented 7 years ago

interesting issue, thanks for reporting

gabordemooij commented 7 years ago

since I have my own company now I have very little time, feel free to issue a patch.

gabordemooij commented 7 years ago

Going to look into this now...

gabordemooij commented 7 years ago

fixed