Query result field type in yii2 database

The default return from the yii2 database query is an AR object, where the field type is essentially the same as the database, but if asArray is used to return in an array, the default field type is all string, and if this json_encode is returned to the App end, it will be killed by the engineers of the strongly typed languages Android/IOS, and php will be pushed to the air outlet again.

The reason is that pdo was not perfect when yii2 was prevalent, and some features were added later, such as the type of result data we urgently needed to return that was consistent with the database.

<?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 adds two configurations of attributes to the database component configuration.

When not configured:

// attributes of AR objects
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)

After Configuration

// attributes of AR objects
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

You can find:
1. By default, an AR object will be able to match the field type as closely as possible to the database, but the id is still a string type.
2. When returning results as arrays, yii2 treats all fields as string s by default, and configures the pdo attribute to completely match the field type with the database.

The session id for querying AR objects after configuring the pdo attribute is not identical to the database field type, but the array query is identical.

Moreover, array queries save memory and perform better (yii2 always queries in array mode. If asArray mode is not used, the result set of the array queried will be mapped to the corresponding AR object in combination with the corresponding Model, that is, asArray actually turns off the step of mapping the array to the AR object. Reference ), we provide data for App by itself to provide some data scalars, there is no need to query data objects, so interface should query data in the way of asArray, and configure PDO properties to maintain consistency of data field types and avoid confusion on docking.

Keywords: Programming Database PDO PHP Attribute

Added by Eratimus on Mon, 30 Dec 2019 09:49:12 +0200