第二个数据库yii2

时间:2018-01-18 09:23:30

标签: php yii2 yii2-advanced-app

我在yii2上有一个网站。我需要连接到远程数据库来获取产品。对于本地数据库,我做:

$query = new Query;

$query->select(['product_sku'])
      ->from('ntc7r_virtuemart_products')
      ->where(['virtuemart_product_id' => $product_id]);

但是当我尝试通过代码连接到远程数据库时:

$query = Yii::$app->db2->createCommand((new \yii\db\Query)->select(['product_sku'])->from('ntc7r_virtuemart_products'))->where(['virtuemart_product_id' => $product_id])->queryAll()

我给出了一个错误 Object of class yii\db\Query could not be converted to string

1 个答案:

答案 0 :(得分:1)

你可以这样做

   (new Query())->select(['product_sku'])->from('ntc7r_virtuemart_products')->where(['virtuemart_product_id' => $product_id])->all(Yii::$app->db2);