Aleksandr Sasha Aleksandr Sasha - 1 year ago 338
PHP Question

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])

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.

The question is, is it
possible to set session group_concat_max_len in Yii2 framework?

Answer Source

You can access your application's database connection object using Yii::$app->db. You can then set your mysql variable just before the call to find:

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.

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

You can then call the query above using

    'SET group_concat_max_len = :length', [
        ':length' => Yii::$app->params['group_concat_max_len']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download