Search code examples
phpmysqlyiiyii1.x

How to re-connect to default db in Yii1?


In config/main.php there’s a ‘db’ component with parameters and such, so in my app it is accessed as Yii::app()->db. So far, it is pretty standard. Now, question - how I can re-initialize it in Yii1?

The problem is that I’m working on a CRON script in which I transfer the files on another server using ftp. At some point it takes many time. After that the script tries to do some db operations and got an error that db connection is gone away. Since this ->db property is a read-only, I can’t just open a new one and re-assign it.

Error : exception 'CDbException' with message 'CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away.

So far my workaround is when I need to do some sql, I just create a new connection using. But there should be a proper way, right?

Again, the question is, how I can re-open the default db connection using parameters specified in the config file?

As I need to do it on urgent.Please help me.


Solution

  • Using this lines of code in Yii1. Yii::app()->db->setActive(false); Yii::app()->db->setActive(true); Its working.