PHP warning Attempt to assign property of non-object

Hi, all.

I’m now trying to use “CSqlDataProvider”, but I’ve got some database connections. like


'db2'=>array(

                        'connectionString' => 'mysql:host=192.168.56.103;dbname=test',

                        'emulatePrepare' => true,

                        'username' => 'test',

                        'password' => 'test',

                        'charset' => 'utf8',

                        'class' => 'CDbConnection',

                ),

.. more db components ..

I would like to code my controller like this:





                $connection=Yii::app()->db2;


                $count=$connection->createCommand('SELECT COUNT(idx) FROM mytable')->queryScalar();


                $sql='SELECT * FROM mytable';


                $dataProvider=new CSqlDataProvider($sql, array(

                                        'db'=>'db2',

                                        'totalItemCount'=>$count,

                                        'sort'=>array(

                                                'attributes'=>array(

                                                        'id',

                                                        ),

                                                ),

                                        'pagination'=>array(

                                                'pageSize'=>10,

                                                ),

                                        ));


                $this->render('index', array('dataProvider'=>$dataProvider)); 

It has no problem before redering view




<?php

$this->widget('zii.widgets.grid.CGridView', array(

                            'dataProvider'=>$dataProvider,

                        ));

?>

When calling method like $dataProvider->getData(); I’ve got an error message.

[color="#FF0000"]Attempt to assign property of non-object[/color] at the code [color="#FF0000"]"$db->active=true;"[/color]

It seems that my $db instance does not have "active" member field. How come???

I found an answer. I’m such a stupid. :-[

It must be ‘db’=>$connection; not ‘db’=>‘db2’.

I passed string instead of instance.