Closed malja closed 7 years ago
Hi, I have an error with this enhancement, I belive that is not correct.
Error:
Array to string conversion
return $query->scalar( Yii::$app->$settings['connection'] ); Line 251 kartik-v\yii2-dynagrid\DynaGridStore.php
Could you please provide your dynagrid part of $config? Key 'connection' should contain string which corresponds to database component name.
This is my dynagrid configuration, which seems to work fine:
'dynagrid'=> [
'class'=>'\kartik\dynagrid\Module',
'dynaGridOptions' => [
'storage' => 'db'
],
'dbSettings' =>[
'tablename' => 'mya2billing.tbl_dynagrid',
'connection' => "mysql_local"
],
'dbSettingsDtl' => [
'tablename' => 'mya2billing.tbl_dynagrid_dtl',
'connection' => "mysql_local"
],
],
And component configuration:
'components' => [
'mysql_local' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=<IP_ADDRESS>;dbname=<DB_NAME>',
'username' => '<USER_NAME>',
'password' => '<PASSWORD>',
'charset' => 'utf8',
],
],
Hi malja, If I apply the changes in two files I give this error, not before.
dynagrid=> [ 'class'=>'\kartik\dynagrid\Module', 'maxPageSize'=>1000, 'dbSettings'=>[ 'connection' => 'db', 'tableName'=>'web_tbl_dynagrid' ], 'dbSettingsDtl'=>[ 'connection' => 'db', 'tableName'=>'web_tbl_dynagrid_dtl' ], 'dynaGridOptions'=>[ 'storage' => 'db', 'toggleButtonGrid'=>['class'=>'btn-sm'], 'toggleButtonFilter'=>['class'=>'btn-sm'], 'toggleButtonSort'=>['class'=>'btn-sm'], 'gridOptions'=>[ 'pjax' => false, 'responsive' => true, 'responsiveWrap' => false, 'export' => [ 'options'=>['class'=>'btn btn-sm btn-default'], 'label' => 'Pagina', 'fontAwesome' => true, 'target' => ExportMenu::TARGET_BLANK, 'showConfirmAlert'=>false, ], ] ] ],
And component configuration:
'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'sqlsrv:Server=<SERVER>;Database=<DB_NAME>;ConnectionPooling=0', 'username' => '<USER>', 'password' => '<PASS>', 'charset' => 'utf8', 'enableSchemaCache' => true, 'schemaCacheDuration' => 3600, 'schemaCache' => 'cache', ],
Weird, first I thought you just set the 'connection' wrong, however your config looks fine. Which PHP version do you use? They are using some old-school 5.something version in company I work for. I will test it at home with newer version of PHP server.
Would you mind adding those two lines just above the line which produces the error message. Then send me the result.
var_dump( $settings['connection'] );
var_dump( get_class( Yii::$app->$settings['connection'] ) );
Note this will add two or more lines to output right next to all of your dynagrids. Do not put it on production server!
Your function getDataFromDb() should look like:
protected function getDataFromDb($col, $id) {
$settings = $this->_isMaster ? $this->_module->dbSettings : $this->_module->dbSettingsDtl;
$query = (new Query())
->select($settings[$col])
->from($settings['tableName'])
->where([$settings['idAttr'] => $id]);
// .. follows debug code
var_dump( $settings['connection'] );
var_dump( get_class( Yii::$app->$settings['connection'] ) );
// .. end of debug code
return $query->scalar( Yii::$app->$settings['connection'] );
}
Ok, the version that I use is 7.0.12 and 7.1.5.
The problem is the access to Yii::$app->(problem to access array )
The first var_dump = 'db', but second show the error Array to string conversion
if I access Yii::$app->db
, it is correct, but if I access Yii::$app->$settings['connection'] )
show error.
To solve it, use new variable:
$db=$settings['connection'];
return $query->scalar( Yii::$app->$db );
And in the function getDtlList
$db=$s['connection'];
$data = (new Query())
->select([$s['idAttr'], $s['nameAttr']])
->from($s['tableName'])
->where([$s['dynaGridIdAttr'] => $this->_mstKey, $s['categoryAttr'] => $cat])
->all( Yii::$app->$db );
This solve the problem.
Fixed via latest commit.
This small change adds key 'connection' to dynagrid settings.
This key is used in DynaGridStore to get data from selected database.