Laravel雄辩的对象,长文本被截断 [英] Laravel Eloquent object, longtext being truncated

查看:87
本文介绍了Laravel雄辩的对象,长文本被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel,将db行加载到Eloquent对象中.其中一列是longtext,即JSON编码的数组,其长度大于2百万个字符.我最初遇到的错误是json_decode在此列的值上失败.

I've using Laravel, loading a db row into an Eloquent object. One of the columns is longtext, a JSON encoded array > 2 million characters long. The original error I was getting was json_decode failing on this column's value.

我在修补匠中进行了测试.简化的测试代码:

I tested in tinker. Simplified test code:

$item = Item::find(1);
echo $item->long_text_field;
var_dump(json_decode($item->long_text_field));
echo strlen($item->long_text_field);

在我的本地无业游民实例上,这显示了正确的值.

On my local vagrant instance this shows the correct values.

...long json array in text, same as the value in the actual DB entry...
...var_dump of json array...
2334040

但是我在远程开发服务器上却得到了

However on my remote dev server I'm getting

...long json array truncated in the middle...
NULL
1048576

很显然json_decode失败,因为字符串被截断了.

Obviously the json_decode is failing because the string is truncated.

它会像这样被截断

"Eff Date":"1\"

应该是哪个

"Eff Date":"1\/30\/14 16:13"

在长文本中有很多类似的转义斜线,而在那时我看不到任何奇怪的字符.有谁知道为什么这段文本会在一台服务器上而不是另一台服务器上被截断?

There's a lot of escaped slashes like that in the longtext, and no strange characters at that point I can see. Does anyone have an idea why this text would truncate like that on one server and not another?

推荐答案

问题是默认的PDO::MYSQL_ATTR_MAX_BUFFER_SIZE大小为1Mb.

The problem is the default PDO::MYSQL_ATTR_MAX_BUFFER_SIZE size is 1Mb.

要在Laravel中进行设置,您需要在database.php配置文件中添加一个选项.

To set this in Laravel you need to add an option to your database.php config file.

'connections' => [
    'mydb' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'mydb',
        'options'   => [
            PDO::MYSQL_ATTR_MAX_BUFFER_SIZE => 16777216
        ]
    ]
]

以上将最大属性大小设置为16Mb.

The above will set the max attribute size to 16Mb.

请注意,如果您正在使用mysqlnd驱动程序,则不再需要此驱动程序,并且由于PDO :: MYSQL_ATTR_MAX_BUFFER_SIZE常量不存在,它实际上会破坏您的代码.

Note that if you're using mysqlnd driver you don't need this anymore and it'll actually break your code as the PDO::MYSQL_ATTR_MAX_BUFFER_SIZE constant doesn't exist.

这篇关于Laravel雄辩的对象,长文本被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆