Restfullyii Has Problems With Binary(16) Column

So, I have these models where the PK’s are all binary(16) columns. I’ve had several problems because of them in the Yii framework itself (see links below):

http://www.yiiframework.com/forum/index.php/topic/32490-binary-string-as-primary-key-throws-error-in-cgridview/page__p__156417__hl__uuid#entry156417

http://www.yiiframework.com/forum/index.php/topic/48135-handling-uuid-pk-column-in-yii/

However, I’ve been able to work around these problems. Now, I started using RESTfullYii (v1.15) and I’ve hit a road block again. Problem in occuring where it is trying to convert this model into json string (in protected/extensions/restfullyii/views/api/output.php). CJON::encode() doesn’t like binary strings and it returns the following error:




json_encode() [function.json-encode]: Invalid UTF-8 sequence in argument



In order to work around this problem, I implemented the following rule in my Model:




array('user', 'filter', 'filter'=>'UUIDUtils::bin2hex'),



This doesn’t fix the problem because RESTFullYii is not calling validate() on the model before trying to encode them into JSON. The responsible method is MorrayBehavior::toArray().

I do not see a way to extend MorrayBehavior without modifying some code in RESTFullYii. Also, I’m not sure if my observations (about not calling validate() to trigger rule chain) are correct.

Any help will, as always, be appreciated.

You should create an abstraction layer between the storage (DB) and the application that would convert between binary and back. Try overloading the afterFind() and beforeSave() methods to do that.

I’ve read the forum posts you linked and it looks like extending some base schema classes is the best way to go if you want methods like findByPk to work.

Thanks for your response.

I have created two virtual attributes that hold hex values of the binary columns. I set and get using these virtual attributes and it most of yii works without problems.

For RESTFullYii, I had to add the following rule to the ‘rest’ scenario:




array('name, uuid, userUUID', 'safe', 'on'=>'rest'),



This ensures that only the hexadecimal string version of the uuid (binary) columns are used and everyone seems to be happy.