yii2 数据库查询结果字段类型的问题

栏目: PHP · 发布时间: 6年前

内容简介:原因是

yii2 数据库查询默认返回的为 AR 对象,此时字段类型与数据库的基本相符,但如果使用 asArray 以数组的方式返回时,默认字段类型全都是 string ,如果这样 json_encode 后返回给 App 端的话,会被 Android/IOS 这些强类型语言端的工程师们喷死, php 会再次被推向风口。

原因是 pdoyii2 盛行时还不够完善,一些特性是后期加进来的,比如我们急切需要的返回的结果数据类型与数据库一致。

<?php

return [
    'class'       => 'yii\db\Connection',
    'dsn'         => 'mysql:host=localhost;dbname=yii2basic',
    'username'    => 'root',
    'password'    => '123456',
    'charset'     => 'utf8',
    'tablePrefix' => 'yii_',
    'attributes'  => [
        PDO::ATTR_STRINGIFY_FETCHES => false,
        PDO::ATTR_EMULATE_PREPARES  => false,
    ]
    // Schema cache options (for production environment)
    //'enableSchemaCache' => true,
    //'schemaCacheDuration' => 60,
    //'schemaCache' => 'cache',
];

yii2 在数据库组件配置中添加 attributes 的两项配置即可。

未配置时:

// AR 对象的 attributes
array (size=7)
  'id' => string '1' (length=1)
  'account' => string 'big_cat' (length=7)
  'password' => string 'e10adc3949ba59abbe56e057f20f883e' (length=32)
  'age' => int 29
  'created_at' => string '2019-05-16 11:23:26' (length=19)
  'updated_at' => string '2019-05-16 11:23:26' (length=19)
  'status' => int 1

// asArray
array (size=7)
  'id' => string '1' (length=1)
  'account' => string 'big_cat' (length=7)
  'password' => string 'e10adc3949ba59abbe56e057f20f883e' (length=32)
  'age' => string '29' (length=2)
  'created_at' => string '2019-05-16 11:23:26' (length=19)
  'updated_at' => string '2019-05-16 11:23:26' (length=19)
  'status' => string '1' (length=1)

配置后

// AR 对象的 attributes
array (size=7)
  'id' => string '1' (length=1)
  'account' => string 'big_cat' (length=7)
  'password' => string 'e10adc3949ba59abbe56e057f20f883e' (length=32)
  'age' => int 29
  'created_at' => string '2019-05-16 11:23:26' (length=19)
  'updated_at' => string '2019-05-16 11:23:26' (length=19)
  'status' => int 1

// asArray
array (size=7)
  'id' => int 1
  'account' => string 'big_cat' (length=7)
  'password' => string 'e10adc3949ba59abbe56e057f20f883e' (length=32)
  'age' => int 29
  'created_at' => string '2019-05-16 11:23:26' (length=19)
  'updated_at' => string '2019-05-16 11:23:26' (length=19)
  'status' => int 1

可以发现:

1、AR 对象默认就能将字段类型与数据库中尽可能的一致,但 id 还是 string 类型。

2、以数组的方式返回结果时, yii2 默认将所有字段都以 string 方式处理,通过配置 pdo 属性能完全的将字段类型同数据库一致。

配置 pdo 属性后查询 AR 对象的话 id 还是没有同数据库字段类型一致,但数组方式查询则完全一致了。

而且数组查询方式更节省内存,性能更高( yii2 其实始终以数组模式查询,如果不使用 asArray 模式,会对查询到的数组结果集结合对应的 Model 进行映射成相应的 AR 对象,即 asArray 其实是关闭了数组映射至 AR 对象的步骤, 参照 ),我们为 App 提供数据时本身就是要提供一些数据标量,没必要查询数据对象,所以接口返回数据时都应该以 asArray 的方式查询,且配置 PDO 的属性以便保持数据字段类型的一致性,避免对接上的混乱。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

区块链与人工智能:数字经济新时代

区块链与人工智能:数字经济新时代

高航、俞学劢、王毛路 / 电子工业出版社 / 2018-7-23 / 80

《区块链与人工智能》是畅销书《区块链与新经济:数字货币2.0时代》全新修订升级版。本书是市场上为数不多的系统阐述区块链、人工智能技术与产业的入门级系统教程。从比特币到各类数字货币(代币),从基础原理到应用探讨,全景式呈现区块链与人工智能的发展脉络,既有历史的厚重感也有科技的未来感。本书的另一个亮点是系统整理了区块链创业地图,是一本关于区块链创业、应用、媒体的学习指南,以太坊创始人Vitalik专门......一起来看看 《区块链与人工智能:数字经济新时代》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具