PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 30, 2021

[FIXED] How to re-connect to default db in Yii1?

 December 30, 2021     mysql, php, yii, yii1.x     No comments   

Issue

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.



Answered By - Nisha Patel
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing