Yii2 user组件 $identity->id 取出来是字符串的问题 - 刨根问底

技术分享 · Fecmall · 于 5年前 发布 · 2525 次阅读

发现该问题是由帖子:http://www.fecshop.com/topic/833

在产品收藏页面,从user组件中取出来的id是string,而不是int,

[@myred08](/member/myred08) 追踪了好久,最终发现是表结果中,id列(主键),加入unsigned导致的

CREATE TABLE IF NOT EXISTS `customer` (
			  `id` int(20) unsigned NOT NULL AUTO_INCREMENT,
			  `password_hash` varchar(80) DEFAULT NULL COMMENT '密码',

unsigned 就是上面id后面的字符,如果是32位机器,加上了unsigned,id就会变成字符串。

具体原因分析:

1.php的环境变量资料:

PHP_INT_SIZE:表示整数integer值的字长

PHP_INT_MAX:表示整数integer值的最大值

注:

输出下32位中PHP_INT_SIZE:4,PHP_INT_MAX:2147483647
输出下64位中PHP_INT_SIZE:8,PHP_INT_MAX:9223372036854775807

2.查看:yii\db\Schema

/**
     * Extracts the PHP type from abstract DB type.
     * @param ColumnSchema $column the column schema information
     * @return string PHP type name
     */
    protected function getColumnPhpType($column)
    {
        static $typeMap = [
            // abstract type => php type
            self::TYPE_TINYINT => 'integer',
            self::TYPE_SMALLINT => 'integer',
            self::TYPE_INTEGER => 'integer',
            self::TYPE_BIGINT => 'integer',
            self::TYPE_BOOLEAN => 'boolean',
            self::TYPE_FLOAT => 'double',
            self::TYPE_DOUBLE => 'double',
            self::TYPE_BINARY => 'resource',
            self::TYPE_JSON => 'array',
        ];
        if (isset($typeMap[$column->type])) {
            if ($column->type === 'bigint') {
                return PHP_INT_SIZE === 8 && !$column->unsigned ? 'integer' : 'string';
            } elseif ($column->type === 'integer') {
                return PHP_INT_SIZE === 4 && $column->unsigned ? 'string' : 'integer';
            }

            return $typeMap[$column->type];
        }

        return 'string';
    }

因此,在yii2中,当user表的id,新建表的时候加入了unsigned,并且是32位机器,id就会被搞成string。

感谢 @myred08 研究了好长时间得出的结果。 很赞~

共收到 8 条回复
myred08#15年前 0 个赞

楼主更正一下哦。我的情况是加了unsigned。才有int变string的问题。

ALTER TABLE  `customer` CHANGE  `id`  `id` INT( 20 ) NOT NULL AUTO_INCREMENT

我是把 unsigned 清除,你的系统本来就是有 unsigned

Fecmall#25年前 0 个赞

@myred08 #1楼 你mysql什么版本?我的加了,没有问题。

算了这个根据自己情况弄吧,我这里就先这样记录一下

myred08#35年前 0 个赞
mysql  Ver 14.14 Distrib 5.5.53, for Win32 (AMD64)

Connection id:          135
Current database:
Current user:           root@localhost
SSL:                    Not in use
Using delimiter:        ;
Server version:         5.5.53 MySQL Community Server (GPL)
Protocol version:       10
Connection:             localhost via TCP/IP
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:               3306
myred08#45年前 1 个赞

https://blog.csdn.net/csssy003/article/details/41516677

最近在做一个restful风格的项目,发现有个表返回的json数据中id始终是string型,但另一个表的id始终是int.

后来我发现原因是出在yii\db\Schema类的getColumnPhpType函数中,这个函数决定了最终出来的数据的类型,其中有一行是这样写的:return PHP_INT_SIZE == 4 && $column->unsigned ? 'string' : 'integer';

就是说如果PHP_INT_SIZE是4并且DB表中对应的列是无符号整形的话就返回string类型.按照php.net上的解释:PHP 不支持无符号整数,因为如果数据库对应字段是无符号的话PHP查出来的值还是转换成int的话就有可能溢出了.

我echo 了我的PHP_INT_SIZE果然是4,又看了下表中id字段果然是无符号型.花了我半下午才搞明白....

Fecmall#55年前 0 个赞

哈哈,释然了,你的机器是32位的吧?

PHP_INT_SIZE:表示整数integer值的字长

PHP_INT_MAX:表示整数integer值的最大值

注:

输出下32位中PHP_INT_SIZE:4,PHP_INT_MAX:2147483647
输出下64位中PHP_INT_SIZE:8,PHP_INT_MAX:9223372036854775807

yii\db\Schema

/**
     * Extracts the PHP type from abstract DB type.
     * @param ColumnSchema $column the column schema information
     * @return string PHP type name
     */
    protected function getColumnPhpType($column)
    {
        static $typeMap = [
            // abstract type => php type
            self::TYPE_TINYINT => 'integer',
            self::TYPE_SMALLINT => 'integer',
            self::TYPE_INTEGER => 'integer',
            self::TYPE_BIGINT => 'integer',
            self::TYPE_BOOLEAN => 'boolean',
            self::TYPE_FLOAT => 'double',
            self::TYPE_DOUBLE => 'double',
            self::TYPE_BINARY => 'resource',
            self::TYPE_JSON => 'array',
        ];
        if (isset($typeMap[$column->type])) {
            if ($column->type === 'bigint') {
                return PHP_INT_SIZE === 8 && !$column->unsigned ? 'integer' : 'string';
            } elseif ($column->type === 'integer') {
                return PHP_INT_SIZE === 4 && $column->unsigned ? 'string' : 'integer';
            }

            return $typeMap[$column->type];
        }

        return 'string';
    }

到这里问题的根找到了,赞~,欣赏你刨根问底的劲。

Fecmall#65年前 1 个赞

我机器是64位, 输出了PHP_INT_SIZE,值为8, 所以没有问题。

myred08#75年前 1 个赞

恩。圆满解决。

kaykay012#85年前 1 个赞

问题和我遇到的一模一样,太6了,完美解决

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics