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

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

内容简介:原因是

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


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

查看所有标签

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

穿越计算机的迷雾

穿越计算机的迷雾

李忠 / 电子工业出版社 / 2011-1 / 36.00元

《穿越计算机的迷雾》从最基本的电学知识开始,带领读者一步一步、从无到有地制造一台能全自动工作的计算机。在这个过程中,读者可以学习到大量有趣的电学、数学和逻辑学知识,了解到它们是如何为电子计算机的产生创造条件,并促使它不断向着更快、更小、更强的方向发展。通过阅读《穿越计算机的迷雾》,读者可以很容易地理解自动计算实际上是如何发生的,而现代的计算机又是怎么工作的。以此为基础,在《穿越计算机的迷雾》的后面......一起来看看 《穿越计算机的迷雾》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器