Yii:在CGridView中显示第二个DB内容

问题描述 投票:0回答:1

我正在使用Yii-1.1.13。我有两个数据库,我已在protected/config/main.php中配置如下。

  'db2'=>array(
   'class'   => 'CDbConnection' ,
   'connectionString' => 'pgsql:host=localhost;port=5432;dbname=database2',
   'emulatePrepare' => true,
   'username' => 'zzzzz',
   'password' => 'zzzzz',
   'charset' => 'utf8',
  ),

  'db'=>array(
   'connectionString' => 'pgsql:host=localhost;port=5432;dbname=database1',
   'emulatePrepare' => true,
   'username' => 'xxxxx',
   'password' => 'xxxxx',
   'charset' => 'utf8',
  ),

我正在使用下面的代码显示Yii CGridView中的计数,它工作正常。这将连接database1。

楷模

 <?     
public function search()
{       
 $criteria=new CDbCriteria;
 $criteria->select='std_id ,count(*) as counts';
 $criteria->condition = "sdate between '$this->startdate' and '$this->enddate'";
 $criteria->group ='std_id';
 return new CActiveDataProvider($this, array( 
    'criteria'=>$criteria,      
    'pagination' => array( 'pageSize' => 30 ),
    ));                         
}       
?>

视图

<?php $this->widget('zii.widgets.grid.CGridView', array(
   'id'=>'std_id-grid', 
   'dataProvider'=>$model->search(),
   'columns'=>array(    
    'std_id',                   
    'counts',                   
    ),                          

   )); ?>               

在另一个模型中,我有下面的代码来显示将与database2连接的用户详细信息。它没有用,有什么不对。我试过两种不同的方式。请参阅下面的Type-1和Type-2。

我已经引用了this链接来连接Yii中的多个数据库。

楷模

    <?

         class Usr extends SecDB
    {
    ...
    ..
             public function getDbConnection()
            {
                return self::getSecDbConnection();
        //We need to override this method in the models representing the 
    //advertising database to return the second DB connection.
        }


    public function search()
    {

     // Type -1 Not working
     /*$row = Yii::app()->db2->createCommand(array(
        'select' => array('id', 'name', 'date_created'),
        'from' => 'accounts',
        'where' => "type = 'USERS'",
        ))->queryAll();

     return new CActiveDataProvider($this, array(
        'criteria'=>$row,
        ));
    */

     // Type -2 Not working

     $criteria=new CDbCriteria;
     $criteria->select='id,name,date_created';
     $criteria->condition = "type = 'USERS'";
     return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,

  ));
    }

 ?>
}

视图

<?php $this->widget('zii.widgets.grid.CGridView', array(
   'id'=>'users-grid',
   'dataProvider'=>$model->search(),
   'columns'=>array(
    'id',
    'name',
    'date_created'
    ),

   )); ?>

我已经覆盖了数据库连接。但它不起作用,浏览器中没有任何内容。


我在protected / components / SecDB.php中有SecDB.php

<?php

class SecDB extends CActiveRecord {
    private static $db2 = null;

    public static function getSecDbConnection()
    {
        if (self::$db2 !== null){
            return self::$db2;
        }else
 {      
            self::$dbcc = Yii::app()->db2;
            if (self::$db2 instanceof CDbConnection)
            {
                self::$db2->setActive(true);
                return self::$db2;
            }
            else
                throw new CDbException(Yii::t('yii','Active Record requires a "db2" CDbConnection application component.'));
        }
    }

}

?>
php yii grid
1个回答
-1
投票

您需要在模型中重写getDBConnection()

public function getDbConnection()
    {
        if (self::$conection !== null)
            return self::$conection;
        else
        {
            self::$conection = Yii::app()->db2;
            if (self::$conection instanceof CDbConnection)
            {
                self::$conection->setActive(true);
                return self::$conection;
            }
            else
                throw new CDbException(Yii::t('yii','Active Record requires a "db2" CDbConnection application component.'));
        }
    }

对于开发模式,在web / index.php中添加

defined('YII_DEBUG') or define('YII_DEBUG',true);

编辑:

<?php

class SecDB extends CActiveRecord {
    private static $db2;

    public function getDbConnection()
    {
        if (self::$db2 !== null){
            return self::$db2;
        }else
 {      
            self::$db2 = Yii::app()->db2;
            if (self::$db2 instanceof CDbConnection)
            {
                self::$db2->setActive(true);
                return self::$db2;
            }
            else
                throw new CDbException(Yii::t('yii','Active Record requires a "db2" CDbConnection application component.'));
        }
    }

}

?>
© www.soinside.com 2019 - 2024. All rights reserved.