cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG]: Column::TYPE_BINARY and Column::TYPE_TINYINTEGER are both 26

Open jturbide opened this issue 2 years ago • 1 comments

Describe the bug Phalcon\Db\Column::TYPE_BINARY and Phalcon\Db\Column::TYPE_TINYINTEGER should not be equal

To Reproduce

Column::TYPE_BINARY === Column::TYPE_TINYINTEGER // true

Expected behavior

Column::TYPE_BINARY === Column::TYPE_TINYINTEGER // false

Additional context Column::TYPE_BINARY is not numeric Column::TYPE_TINYINTEGER is numeric Missing unit test for TYPE_BINARY

jturbide avatar Feb 18 '24 21:02 jturbide

The biggest issue is when we are using casting (i.e. castOnHydrate) it will convert my binary into a int because it found the wrong type. Since I don't want to turn off casting on hydrate, and want to fix the bind type, tis is my temporary solution for now. I made my own Mysql adapter to get around the issue affected by the wrong Column type value.

use Phalcon\Db\Column;

class Mysql extends \Phalcon\Db\Adapter\Pdo\Mysql
{
    public function describeColumns(string $table, string $schema = null): array
    {
        $definitions = parent::describeColumns($table, $schema);
        
        if (Column::TYPE_TINYINTEGER !== Column::TYPE_BINARY) {
            return $definitions;
        }
        
        foreach ($definitions as $key => $definition) {
            
            if ($definition->getType() === Column::TYPE_TINYINTEGER && !$definition->isNumeric()) {
                // probably a binary at this point
                
                $newDefinition = [];
                
                // protected to public
                $prefix = chr(0).'*'.chr(0);
                foreach ((array)$definition as $k => $value) {
                    $newDefinition[str_replace($prefix, '', $k)] = $value;
                }
                
                $newDefinition['bindType'] = Column::BIND_PARAM_BLOB;
                $newDefinition['type'] = Column::TYPE_VARBINARY;
                unset($newDefinition['scale']);
                
                // reset definition
                $definitions[$key] = new Column($definition->getName(), $newDefinition);
            }
        }
        
        return $definitions;
    }
}

jturbide avatar Feb 18 '24 23:02 jturbide

This has been fixed in https://github.com/phalcon/cphalcon/pull/16564

Thank you @jturbide

niden avatar Apr 05 '24 13:04 niden