2016-09-07 3 views
-1

私は異なるデータベースにレコードを持っています。 findallメソッドが呼び出されたときに、それらのすべてを返す必要があります。Yii:Cactiverecord :: findall()は別のデータベースから検索し、値を返す必要があります

私がモデルにgetDbConnection()関数を持っている

public function getDbConnection($db='') 
{  
    if($db!='') 
    { 
     $connection = Yii::app()->getComponent($db); 
     return $connection; 

    } 

    if(self::$db!==null) 
     return self::$db; 
    else 
    { 
     self::$db=Yii::app()->getDb(); 

     if(self::$db instanceof CDbConnection) 
      return self::$db; 
     else 
      throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.')); 
    } 
    } 
CactiveRecord.php

public function getCommandBuilder($db) 
{ 
    return $this->getDbConnection($db)->getSchema()->getCommandBuilder($db); 
} 


public function findAll($condition='',$params=array()) 
{ 
     Yii::trace(get_class($this).'.findAll()','system.db.ar.CActiveRecord'); 

    $dbconnection = array('db','dbmobisite');//db abd dbmobisite are component names in protected/config/main.php 
    $returnall = array(); 
    foreach($dbconnection as $db) 
    {  
     $criteria=$this->getCommandBuilder($db)->createCriteria($condition,$params); 
     $return = $this->query($criteria,true); 
     foreach($return as $values) 
     { 
      array_push($returnall,$values); 
     } 
    } 

    return $returnall; 
} 

しかし、私は次のようなエラーになっています:

の引数1がありませんがCActiveRecord :: getCommandBuilder()、1358行で/home/jrickby/public_html/hastings.pro/framework/db/ar/CActiveRecord.phpで呼び出され、

を定義し、それはエラーが、私はこの問題を解決する方法を知らないこの機能に

public function getCommandBuilder($db) 
{ 
    return $this->getDbConnection($db)->getSchema()->getCommandBuilder($db); 
} 

であると言い、

答えて

0

を助けてくださいあなたは自分の設定ファイルにデータベース構成アレイを作成することができますし、モデルを右のDB設定に関連付けます。

anotherDb

public function getDbConnection() 
{ 
    return Yii::app()->anotherDb; 
} 
内に存在するモデルで設定/ main.php

'components' => [ 
    'db' => [ 
     ... 
    ], 
    'anotherDb' => [ 
     ... 
    ], 
], 

オーバーライドgetDbConnectionを()

関連する問題