简体   繁体   English

PHP YII2设置会话group_concat_max_len

[英]PHP YII2 set session group_concat_max_len

$model = Item::find()
->select('group_concat(item.ID) AS ItemID')
->joinWith('image', true, 'LEFT JOIN')
->where('item.user_id = :id', [':id' => Yii::$app->user->identity->ID])
->all();

You see, if I do like this it will print me the string of itemID with 145 elements separated by comma, but if there are 3000k records in db, so it will output me only half of them. 您会看到,如果我这样做,它将以逗号分隔的145个元素的形式显示itemID的字符串,但是如果db中有3000k条记录,那么它将仅输出其中一半。

The question is, is it possible to set session group_concat_max_len in Yii2 framework? 问题是,是否可以在Yii2框架中设置会话group_concat_max_len?

You can execute any arbitrary query with Yii2 framework. 您可以使用Yii2框架执行任意查询。

Use something like this for your issue: 使用类似这样的东西来解决您的问题:

// open connection to the DB
$connection = new \yii\db\Connection([
    'dsn' => $dsn,
    'username' => $username,
    'password' => $password,
]);
$connection->open();

// Update the MySQL config
$sql = 'SET group_concat_max_len = 50000;';
$connection->createCommand($sql)->execute();

Be aware that the DB answer will still be truncated if the length of your SELECT response is larger than 50000 (in my example, choose, the value best for you). 请注意,如果SELECT响应的长度大于50000(在我的示例中,选择最适合您的值),则数据库答案仍将被截断。

Be also aware that the effective maximum length of the return value is constrained by the value of max_allowed_packet of your MySQL server. 还应注意,返回值的有效最大长度受MySQL服务器的max_allowed_pa​​cket值限制。

More on this MySQL configuration here: http://dev.mysql.com/doc/refman/5.5/en/group-by-functions.html#function_group-concat 有关此MySQL配置的更多信息,请访问: http : //dev.mysql.com/doc/refman/5.5/en/group-by-functions.html#function_group-concat

You can access your application's database connection object using Yii::$app->db . 您可以使用Yii::$app->db访问应用程序的数据库连接对象。 You can then set your mysql variable just before the call to find : 然后,可以在调用find之前设置mysql变量:

Yii::$app->db->createCommand('SET group_concat_max_len = $length')->execute();
$model = Item::find()
...

For consistency, you can set your group_concat_max_len as a Yii parameter in your config file to make it accessible application-wide. 为了保持一致性,您可以在配置文件group_concat_max_len设置为Yii参数,以使其在整个应用程序范围内均可访问。

'params' => [
    ...
    'group_concat_max_len' => 50000,
]

You can then call the query above using 然后,您可以使用调用上面的查询

Yii::$app->db->createCommand(
    'SET group_concat_max_len = :length', [
        ':length' => Yii::$app->params['group_concat_max_len']
    ]
)->execute();

if you have used ActiveDataProvider , please try this code : 如果您使用过ActiveDataProvider,请尝试以下代码:

$dataProvider = new ActiveDataProvider([
        'query' => $query,



    ]);
    $dataProvider->db->createCommand('SET group_concat_max_len = 1024*4')->execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM