Reputation: 3982
I want to try db connection to check is db available. In zend I can place my code in boostrap file and wrap it in try catch.
How to implement this in yii? Is in yii analog of zend boostrap?
UPD: db is mongo, yii extention for working with db is a directmongosuite
Upvotes: 0
Views: 304
Reputation: 3982
Seems that I find appropriate solution:
Need to prohibit auto connect in config file:
'components' => array( 'edms' => array( 'class' => 'EDMSConnection', 'dbName' => 'homeweb', 'server' => 'mongodb://localhost:27017', 'options' => array('connect' => false) ) )
all controllers should extend one custom controller (BaseController
for example).
Need to write own public function beforeAction
method where I can add boostrap code.
class BaseController extends CController
{
public $layout = '//layouts/main';
public $navigationMenu = array();
public $breadcrumbs = array();
public function beforeAction($action)
{
try {
Yii::app()->edmsMongo()->connect();
} catch (Exception $e) {
die('Cannot connect to the database server. Please Try again later.');
}
$isGuest = Yii::app()->user->isGuest;
$this->navigationMenu = $this->_getNavigationMenu($isGuest);
return parent::beforeAction($action);
}
In the beforeAction
method need to add return true
or execute parent's method.
Upvotes: 1
Reputation: 8408
The bootstrap in yii is pretty much the index.php file under public_html or the yiic.php file (for command line applications).
You will probably have to separate the creating of the application instance and running it (by default it does both on 1 line), so you can do your try/catch between the calls.
Just try to fetch the app component, the mongo plugin will throw an exception if it can't open the connection:
try
{
Yii::app()->mongoDb;
}
...
or Yii::app()->getComponent('mongoDb');
Upvotes: 0