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

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

内容简介:原因是

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 的属性以便保持数据字段类型的一致性,避免对接上的混乱。


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

查看所有标签

猜你喜欢:

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

重构(影印版)

重构(影印版)

Martin Fowler / 中国电力出版社 / 2003-7-1 / 49.00元

随着对象技术应用越来越普及,软件开发社区出现了一个新的问题。缺乏经验的开发者编写出了大批设计较差的程序,导致这些应用程序非常低效,且难于维护和扩展。本书除了讨论重构的各种技巧之外,还提供了超过70个可行重构的详细编目,对如何应用它们给出了有用的提示;并以step by step的形式给出了应用每一种重构的指南;而且用实例展示了重构的工作原理。这些示例都是用Java语言写成的,但其中的思想却可以运用......一起来看看 《重构(影印版)》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具