PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 5, 2022

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

 January 05, 2022     biginteger, cakephp, cakephp-3.0, entity, mysql     No comments   

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?

  • Similar question that didn't receive an answer: Cakephp 3 Bigint issue - Not receiving the same contact number
  • It seems the PHP deployment on my Windows 10 laptop runs in 32 bit. A solution would trying to make my PHP + Apache (XAMPP) run in 64 bit, but I would rather have a general solution.

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

  • Cookbook > Database Access & ORM > Database Basics > Adding Custom Types


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing