Laravel PDO设置? [英] Laravel PDO Settings?
问题描述
我想从数据库中以整数形式返回INT.目前,所有值均以字符串形式加载.数据库是MSSQL 2012,我使用PDO驱动程序(适用于v5.6).
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).
尝试在此处设置属性(如在fideloper.com上的此处所示,但我不知道那是否仍然可能):
Trying to set the properties here (as shown here on fideloper.com, but I don't know if that's still possible):
'sqlsrv' => [
'driver' => 'sqlsrv',
'charset' => 'utf8',
'prefix' => '',
......
'options' => array(
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false,
),
],
但是总是会出错:
SQLSTATE[IMSSP]: The given attribute is only supported on the PDOStatement object.
如何设置PDO驱动程序的任何设置以将INT作为整数而不是字符串返回.
How can I set any settings for the PDO Driver to return INT as Integers and not as Strings.
这仍然不起作用:
$pdo = DB::connection()->getPdo();
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
.. do ORM Query
带来相同的错误.
也许有人可以帮助我吗?
Maybe anyone can help me?
推荐答案
我认为此问题与所使用的PDO驱动程序有关(与PHP一起安装,而不是laravel配置).
I believe this issue is related to the PDO driver used (thats installed with PHP, not laravel configuration).
不是您要找的东西,但有可能解决您的问题.从laravel 5开始,雄辩的语言有了转换功能,您的列会自动转换为您的预定义类型.参见 http://laravel.com/docs/5.0/eloquent#attribute-casting
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
// Eloquent Model
protected $casts = [
'int_column' => 'int',
];
从数据库中检索模型时,
您的 int_column
然后将自动转换为int
Your int_column
would then automatically be cast to an int when the model is retrieved from the database
这篇关于Laravel PDO设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!