Laravel PDO 设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31563747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:00:36  来源:igfitidea点击:

Laravel PDO Settings?

phpsql-serverlaravel

提问by derdida

I would like to return INT as integers from my database. At the moment all values are loaded as Strings. Database is MSSQL 2012 and I use the PDO driver (for v5.6).

我想从我的数据库中以整数形式返回 INT。目前所有值都作为字符串加载。数据库是 MSSQL 2012,我使用 PDO 驱动程序(适用于 v5.6)。

Trying to set the properties here (as shown here on fideloper.com, but I don't know if that's still possible):

尝试在此处设置属性(如fideloper.com 上所示,但我不知道这是否仍然可能):

   'sqlsrv' => [
        'driver'   => 'sqlsrv',
        'charset'  => 'utf8',
        'prefix'   => '',
         ......
        'options'   => array(
            PDO::ATTR_STRINGIFY_FETCHES => false,
            PDO::ATTR_EMULATE_PREPARES => false,
        ),
    ],

But always getting an error:

但是总是报错:

SQLSTATE[IMSSP]: The given attribute is only supported on the PDOStatement object.

How can I set any settings for the PDO Driver to return INT as Integers and not as Strings.

如何设置 PDO 驱动程序的任何设置以将 INT 作为整数而不是字符串返回。

This is still not working:

这仍然不起作用:

 $pdo = DB::connection()->getPdo();
 $pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
 $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
 .. do ORM Query

Bringing the same error.

带来同样的错误。

Maybe anyone can help me?

也许有人可以帮助我?

采纳答案by Wader

I believe this issue is related to the PDO driver used (thats installed with PHP, not laravel configuration).

我相信这个问题与使用的 PDO 驱动程序有关(这是用 PHP 安装的,而不是 Laravel 配置)。

Not quite what you're looking for but could potentially solve your problems. Since laravel 5 theres been a casts feature on eloquent where your columns are automatically cast to your pre-defined types. See http://laravel.com/docs/5.0/eloquent#attribute-casting

不完全是您正在寻找的,但可能会解决您的问题。从 laravel 5 开始,在 eloquent 上有一个强制转换功能,您的列会自动转换为您预定义的类型。见http://laravel.com/docs/5.0/eloquent#attribute-casting

// Eloquent Model
protected $casts = [
    'int_column'   => 'int',
];

Your int_columnwould then automatically be cast to an int when the model is retrieved from the database

int_column当从数据库中检索模型时,您将自动转换为 int

回答by Code Wookiee

I know this is an old thread, but I found a global solution to this issue for MSSQL drivers and PHP 7 (single change that affects all tables / models). Hopefully this will help others that are struggling with the same.

我知道这是一个旧线程,但我为 MSSQL 驱动程序和 PHP 7 找到了针对此问题的全局解决方案(影响所有表/模型的单个更改)。希望这将有助于其他正在努力解决相同问题的人。

  1. Get the latest version of the drivers from the Git Repository (Microsoft/msphpsql). The current version released on the Microsoft Downloads page is an older version and won't work (as of 9/13/2016).
  2. Copy the appropriate DLLs into your php/extfolder (replacing/deleting the older version). You'll probably need to stop/start your web server (definitely, if IIS) to free the original files up for replacement/deletion. If the filenames changed from the previous version you had installed, update your php.inifile.
  3. Update the driver configuration to include the new PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPEparameter:

    'sqlsrv' => [
        'driver' => 'sqlsrv',
        'host' => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'database'),
        'username' => env('DB_USERNAME', 'laravel'),
        'password' => env('DB_PASSWORD', 'password#1'),
        'charset' => 'utf8',
        'prefix' => '',
        'options'   => array(
            PDO::ATTR_STRINGIFY_FETCHES => false,
            PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true
        ),
    ],
    
  1. 从 Git 存储库 ( Microsoft/msphpsql)获取最新版本的驱动程序。Microsoft 下载页面上发布的当前版本是旧版本,无法使用(截至 2016 年 9 月 13 日)。
  2. 将适当的 DLL 复制到您的php/ext文件夹中(替换/删除旧版本)。您可能需要停止/启动您的 Web 服务器(当然,如果是 IIS)以释放原始文件以进行替换/删除。 如果文件名与您安装的先前版本不同,请更新您的php.ini文件。
  3. 更新驱动程序配置以包含新PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE参数:

    'sqlsrv' => [
        'driver' => 'sqlsrv',
        'host' => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'database'),
        'username' => env('DB_USERNAME', 'laravel'),
        'password' => env('DB_PASSWORD', 'password#1'),
        'charset' => 'utf8',
        'prefix' => '',
        'options'   => array(
            PDO::ATTR_STRINGIFY_FETCHES => false,
            PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true
        ),
    ],
    

For explanation of the solution, I found it by working my way through the source code on the Git Repository. Of course it would have been a lot easier if I had read the Announcements section of the README file first:

为了解释该解决方案,我通过查阅 Git Repository 上的源代码找到了它。当然,如果我先阅读自述文件的公告部分会容易得多:

July 28, 2016(4.1.0): Thanks to the community's input, this release expands drivers functionalities and also includes some bug fixes:

  • SQLSRV_ATTR_FETCHES_NUMERIC_TYPEconnection attribute flag is added to PDO_SQLSRV driver to handle numeric fetches from columns with numeric Sql types (only bit, integer, smallint, tinyint, float and real). This flag can be turned on by setting its value in PDO::setAttributeto true, For example, $conn->setAttribute(PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE,true);If SQLSRV_ATTR_FETCHES_NUMERIC_TYPEis set to truethe results from an integer column will be represented as an int, likewise, Sql types float and real will be represented as float. Note for exceptions:
    • When connection option flag ATTR_STRINGIFY_FETCHESis on, even when SQLSRV_ATTR_FETCHES_NUMERIC_TYPEis on, the return value will still be string.
    • When the returned PDO type in bind column is PDO_PARAM_INT, the return value from a integer column will be int even if SQLSRV_ATTR_FETCHES_NUMERIC_TYPEis off.

2016 年 7 月 28 日(4.1.0):感谢社区的投入,此版本扩展了驱动程序功能,还包括一些错误修复:

  • SQLSRV_ATTR_FETCHES_NUMERIC_TYPE连接属性标志被添加到 PDO_SQLSRV 驱动程序以处理从具有数字 Sql 类型(仅位、整数、smallint、tinyint、float 和 real)的列中获取数字。该标志可以通过在设置其值被接通 PDO::setAttributetrue,例如, $conn->setAttribute(PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE,true);如果SQLSRV_ATTR_FETCHES_NUMERIC_TYPE被设置为true从一个整数列的结果作为将被表示int,同样地,SQL类型浮动和实际将表示为float。异常注意事项:
    • 当连接选项标志ATTR_STRINGIFY_FETCHES打开时,即使SQLSRV_ATTR_FETCHES_NUMERIC_TYPE打开,返回值仍然是字符串。
    • 当绑定列中返回的 PDO 类型为 时PDO_PARAM_INT,整数列的返回值即使SQLSRV_ATTR_FETCHES_NUMERIC_TYPE关闭也将为 int 。

回答by Steve Bauman

If anyone is here looking on how to get the SQLSRV PDO driver to return decimal or money values as floats in PHP - unfortunately this isn't possible:

如果有人在这里寻找如何让 SQLSRV PDO 驱动程序以 PHP 中的浮点数形式返回十进制或货币值 - 不幸的是,这是不可能的:

Formatting Decimal Strings and Money Values (PDO_SQLSRV Driver)

To preserve accuracy, decimal or numeric types are always fetched as strings with exact precisions and scales. If any value is less than 1, the leading zero is missing. It is the same with money and smallmoney fields as they are decimal fields with a fixed scale equal to 4.

格式化十进制字符串和货币值(PDO_SQLSRV 驱动程序)

为了保持准确性,十进制或数字类型始终作为具有精确精度和比例的字符串获取。如果任何值小于 1,则缺少前导零。money 和 smallmoney 字段相同,因为它们是固定小数位数等于 4 的十进制字段。

https://docs.microsoft.com/en-us/sql/connect/php/formatting-decimals-pdo-sqlsrv-driver?view=sql-server-2017

https://docs.microsoft.com/en-us/sql/connect/php/formatting-decimals-pdo-sqlsrv-driver?view=sql-server-2017