快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Yii2数据库的INT类型怎么转换为PHP类型

本篇内容主要讲解“Yii2数据库的INT类型怎么转换为PHP类型”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Yii2数据库的INT类型怎么转换为PHP类型”吧!

创新互联建站公司2013年成立,是专业互联网技术服务公司,拥有项目网站设计制作、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元临沂做网站,已为上家服务,为临沂各地企业和个人服务,联系电话:13518219792

Yii2对于数据库的int类型转换到php类型时,会根据server 环境,决定是integer还是string

如果是32位服务器PHP_INT_SIZE=4, 64位服务器,PHP_INT_SIZE=8.

也就是说,如果DB的int类型超过了PHP的int最大值,则会转为string。

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';
}

源码:yii2\db\Schema.php

/**
 * 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数据库的INT类型怎么转换为PHP类型”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


网站标题:Yii2数据库的INT类型怎么转换为PHP类型
当前链接:http://6mz.cn/article/pogehh.html

其他资讯