Yii Framework Forum: PHP warning Attempt to assign property of non-object - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

PHP warning Attempt to assign property of non-object Rate Topic: -----

#1 User is offline   jonghwa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 04-December 11
  • Location:Seoul, Korea

Posted 01 August 2012 - 01:40 AM

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.

Attempt to assign property of non-object at the code "$db->active=true;"

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

#2 User is offline   jonghwa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 04-December 11
  • Location:Seoul, Korea

Posted 01 August 2012 - 01:49 AM

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


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

I passed string instead of instance.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users