Fecshop 2018-05-18 11:35:44 5724次浏览 2条评论 0 8 0

原文链接:Yii2 user组件 $identity->id 取出来是字符串的问题

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

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

追踪了好久,最终发现是表结果中,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。

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

觉得很赞
您需要登录后才可以评论。登录 | 立即注册