Wednesday, January 5, 2022

[FIXED] CakePHP 3 - MySQL 'BIGINT' field not processed correctly in entity

Issue

I have a database where I store MAC addresses. There is quite some discussion about how to store a MAC address in a MySQL database, I decided to store it as an unsigned BIGINT to save a few bytes per row and achieve faster searches.

This would work well, except that when I retrieve an entity from the table the mac address in the entity is limited to 2147483647, e.i. the PHP MAX_INT value for a 32 bit system. So the mac address will not be correct if it exceeds 2147483647 (which will happen for 99% of addresses).

It seems that somewhere in the process from database to entity the value is parsed as int, which breaks the values.

Is there some way of avoiding this? Would it be possible to force the entity property to be a string instead of int?


Solution

The conversion happens in the type layer (\Cake\Database\Type), it's triggered by the query object when it fetches rows.

By default big integers are mapped to the \Cake\Database\Type\IntegerType. If you need to run this on a 32-bit system, then you could for example remap the biginteger type to \Cake\Database\Type\StringType. In your config/bootstrap.php:

Type::map('biginteger', \Cake\Database\Type\StringType::class);

See also



Answered By - ndm

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.